Form data submission to mvc3 controller

I'm new to .net MVC3, so forgive my ignorance. I have a rather large form (many fields), and I'm just wondering if I really need to refer to each of my fields as arguments to my method of action at the rear end, or if you can pass them all in a kind of collection, then refer to the collection to get the values.

If possible, can someone provide a short example of how?

thank

+3
source share
2 answers

The shortest example I can come up with ...

View Model:

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

View:

<%: Html.EditorForModel() %>

controller

[HttpGet]
public ActionResult Person()
{
     return View(new ViewModel());
}
[HttpPost]
public ActionResult Person(ViewModel formData)
{
     // formData is bound already -- just use it!
}
+4
source

You can pass all the data to the controller as a custom type.

public ActionResult MyControllerMethod(MyCustomType formData)

, HtmlHelper, :

<%= Html.TextBoxFor(m => m.FirstName) %>

, model, .

0

All Articles