Click here to Skip to main content
15,891,779 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create simple login page, which will validate user.

In my view
<script type="text/javascript">
    $(document).ready(function () {
        $('#btnLogin').click(function () {
            document.location = '@Url.Action("Validateuser", "Login")';
        });
        $('body').css("background", "#517EAD");
        $('body').css("color", "Green");
        $('body').css("margin", "0");
    });
</script>
<div style="background:white;text-align:right;padding:0px 50px;">
      <h2>Please Login</h2>
</div>

<div style="float:right;">
    <table border="0">
<tr>
    <td>
        <label id="lbllogin">LoginID:</label>        
    </td>
    <td>
            <input type="text" id="txtLogin" required placeholder="Prashant" style="border-radius:5px;background:orange;"/>
    </td>
</tr>
<tr>
    <td></td>
    <td>
        <input type="submit" id="btnLogin" value="Login" style="border-radius:15px;background:silver;"/>
    </td>
</tr>
</table>
</div>


How i can pass textbox value to the controller??

I have on controller with 2 actions as follows
namespace MVC.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index() // has Index.cshtml view
        {
            return View();
        }
        
        public ActionResult Validateuser() // has Validateuser.cshtml view
        {
            Login objLogin = new Login();
            int intUser = objLogin.validateUser();
            //ViewData["Msg"] = "Invalid User";
            return RedirectToAction("Index","Login");
        }
    }
}


In above code if you check, in Validateuser() i have not written any logic but i'm trying to redirect to Index.cshtml view and want to pass "Invalid User" massage to the view where i will display.



can i know how can i do this??

thanks in advance
Posted
Updated 28-Feb-13 2:49am
v2

1 solution

You can use Model,ViewData,ViewBag,TempData for send data to view. I prefer ViewData for your requirement as its type is dynamic. In your code you used ViewBag and though is not working, you commented out ViewBag.
Actually it will work when you use View(YourViewName) method for return ActionResult. If you use RedirectToAction method then it actually use 2 separate request so ViewData/ViewBag value will be lost. If you still need to use ReDirectToAction then you should send data to view with the help of TempData object.

C#
public ActionResult Contact()
{
    ViewBag.Message = "Your contact page.";
    //ViewBag.Hello = "GoodMorning";
    TempData["Hello"] = "Good Morning???";
    return RedirectToAction("About");
}

HTML
<div>@TempData["Hello"]</div>


I prefer you use
C#
public ActionResult Validateuser() // has Validateuser.cshtml view
       {
           Login objLogin = new Login();
           int intUser = objLogin.validateUser();
           ViewBag.Msg = "Invalid User";
           //return RedirectToAction("Index","Login");
           return View("About");
       }


Please visit Link
 
Share this answer
 
v7
Comments
dhage.prashant01 28-Feb-13 6:04am    
I tired using TempData and its working.

When I tried following it gives error
ViewBag.Msg = "Invalid User";
return View("Login");

what is difference between ViewData,ViewBag,TempData??
S. M. Ahasan Habib 28-Feb-13 6:22am    
http://www.codeproject.com/Articles/476967/WhatplusisplusViewData-2cplusViewBagplusandplusTem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900