How to pass a text field \ input values ​​from a view to a controller in MVC

I have the following inputs in my view, and I want to pass them to the controller:

<div> <span><label>Username</label></span> <span><input type="text" class="textbox" id="username"></span> </div> <div> <span><label>Password</label></span> <span><input type="password" class="password" id="password"></span> </div> 

And this is my controller method for authenticating a specific user:

 public ActionResult LoginMethod(string username, string password) { if (username == "admin" && password == "admin1") { return RedirectToAction("Home"); } else { return RedirectToAction("Login"); } } 
+4
source share
3 answers

If you want to save the controller action and view it, you can simply add the name property so that it matches your actual parameters and they will pass data through:

 <input type="text" class="textbox" id="username" name="username"> <input type="password" class="password" id="password" name="password"> 

However, I would recommend using strongly typed view models, since there is less room for errors and uses the framework better.

To do this, you will do the following:

Create a class containing your properties and add DisplayName attributes for your labels:

 public class FooViewModel { [DisplayName("Username")] public string Username { get; set; } [DisplayName("Password")] public string Password { get; set; } } 

Add the model directive to your view and use HtmlHelpers instead of html inputs:

 @model FooViewModel <div> <span>@Html.LabelFor(x => x.Username)</span> <span>@Html.TextBoxFor(m=> m.Username)</span> </div> <div> <span>@Html.LabelFor(x => x.Password)</span> <span>@Html.PasswordFor(m=> m.Password)</span> </div> 

Then change your action to the following:

 public ActionResult LoginMethod(FooViewModel model) { if (model.Username == "admin" && model.Password == "admin1") { return RedirectToAction("Home"); } else { return RedirectToAction("Login"); } } 
+4
source

As a rule, it is better to use the viewmodel to send data back, it also allows you to perform some validation.

So you define the model:

 public class MyViewModel { [Required] [StringLength(50)] [Display(Name = "Username")] public string Username { get; set; } [Required] [StringLength(50)] [Display(Name = "Password")] public string Passsword { get; set; } } 

So your markup becomes:

 @model MyViewModel @using(Html.BeginForm()) { <div> <span>@Html.LabelFor(m => m.Username)</span> <span>@Html.TexboxFor(m => m.Username)</span> Html.ValidationMessageFor(x => x.Username) </div> <div> <span>@Html.LabelFor(m => m.Password)</span> <span>@Html.PasswordFor(m => m.Password)</span> Html.ValidationMessageFor(x => x.Password) </div> } 

And your controller action:

 public ActionResult LoginMethod(MyViewModel myViewModel) { if (myViewModel.Username == "admin" && myViewModel.Password == "admin1") { return RedirectToAction("Home"); } else return RedirectToAction("Login"); } 

You can make it work by setting the name attribute to match the parameters of the action or the properties of the view mode, but with the help of viewmodel and helpers it is much more reliable and easier to refactor and also to check.

+1
source

First you must put your inputs in the form, for example,

 <% Html.BeginForm("LoginMethod", "Authentication");%> Username: <input type="text" id="username" name="username" /> Password: <input type="password" id="password" name="password" /> <input type="submit" id="Go" value="Post" /> <% Html.EndForm();%> 

or you can use <%: Html.TextBoxFor(m => m.UserName) %> instead of html inputs.

and here is your controller method;

 public ActionResult LoginMethod(string username,string password) { **do your stuff! } 
0
source

All Articles