Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC3
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 28-Feb-13 0:20am
Edited 28-Feb-13 3:49am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
public ActionResult Contact()
{
    ViewBag.Message = "Your contact page.";
    //ViewBag.Hello = "GoodMorning";
    TempData["Hello"] = "Good Morning???";
    return RedirectToAction("About");
}
<div>@TempData["Hello"]</div>
 
I prefer you use
 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
  Permalink  
v7
Comments
dhage.prashant01 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 341
1 Marcin Kozub 225
2 Praneet Nadkar 197
3 Sergey Alexandrovich Kryukov 190
4 Shweta N Mishra 161
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 28 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100