I like to create an action method for my posts. So let's say you have a UserViewModel:
public class UserViewModel { public int Id { get; set; } public string Name { get; set; } }
Then UserController:
public class UserController { [HttpGet] public ActionResult Edit(int id) {
I assume you already have a form. You want to make sure the form submits data to the correct action method. In my example, you create the form as follows:
@model UserViewModel @using (Html.BeginForm("Edit", "User", FormMethod.Post)) { @Html.TextBoxFor(m => m.Name) @Html.HiddenFor(m => m.Id) }
The key to all this is the binding of the model that MVC performs. Use HTML helpers like the Html.TextBoxFor I used. In addition, you will notice the top line of view code that I added. @model tells you that you will send it a UserViewModel. Let the engine work for you.
Edit: Nice call, it's all in Notepad, forgot HiddenFor for Id!
source share