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"); } }
source share