opel
opel

Reputation: 21

MVC normal html form binding

I've seen thousands of examples using the razor BeginForm and I know that it auto binds the controller input to the submitted info... anyway I want to know if the same auto bind exists in a normal

<form method="get" action="@Url.Action("Index","Home")"> <input type="text" name="foo"> </form> and if it doesn't ... how can I bind it... and please don't tell me to use html helpers...

Upvotes: 1

Views: 1495

Answers (1)

Usman
Usman

Reputation: 4703

yes you can bind model with normal html but the value of the name attribute has to be same as name of the property of the model e.g

public class viewmodel
{
  public string FirstName {get; set;}
  public string  LastName {get; set;}  
}

so your html will be

@model viewmodel

<form method="get" action="@Url.Action("Index","Home")">
      <input type="text" name="FirstName" value="@model.FirstName"> //name should be of same name as property name
      <input type="text" name="LastName" value="@model.LastName">
      <input type='submit' value='Submit'/>
</form>

and finally your action will be

public ActionResult Index(viewmodel model)
{
   return View(model);
}

Upvotes: 3

Related Questions