Click here to Skip to main content
15,896,539 members

How can we pass value from controller to view and vise versa

dhage.prashant01 asked:

Open original thread
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
Tags: MVC3

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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