4

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 in order to authenticate a particular user:

public ActionResult LoginMethod(string username, string password)
{
    if (username == "admin" && password == "admin1")
    {
       return RedirectToAction("Home");
    }
    else 
    {
       return RedirectToAction("Login");
    }
}
1
  • 2
    Your inputs need name attributes which match your property names. But you should be using a model and using strongly typed html helpers to bind to your model, and post back the model. Go to the MVC site and learn the basics. Commented Aug 6, 2015 at 8:20

3 Answers 3

4

If you want to keep your controller action and view the same you can simply add the name property to match your action parameters and they will pass the data across:

<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 as there is less room for error and makes better use of the framework.

In order to do this you would do the following:

Create a class that holds 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 a 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 this:

public ActionResult LoginMethod(FooViewModel model)
{
   if (model.Username == "admin" && model.Password == "admin1")
   {
     return RedirectToAction("Home");
   }
  else 
  {
     return RedirectToAction("Login");
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help, helped me in solving my problem :)
@user3423878 No problem. :)
1

Generally it is better to use a viewmodel to post the data back to, this also lets you do some validation.

So you define a 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 you controller action is:

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

You can get it to work by setting the name attribute to match the parameters to the action, or the properties of the viewmodel but using a viewmodel and helpers is much more robust and easier to refactor, plus validation.

Comments

0

First you should put your inputs in a form like,

<% 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 your Controller method;

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.