I have a Controller with two Edit methods (see below). When I submit the view, any property in quoteOption that is not posted back via an input control is empty. For example, in quoteOption I have quoteOptionID – which I don’t display or make editable. That property is empty (set to 0) in quoteOptionToUpdate. If I add a textbox for QuoteOptionID then it works.
public ActionResult Edit(long id)
{
quoteOption = quoteService.GetQuoteOptionByID(id);
return View("Create",quoteOption);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(QuoteOption quoteOptionToUpdate)
{
quoteService.UpdateQuoteOption(quoteOptionToUpdate); //quoteOptionID is null
return RedirectToAction("Index");
}
In typical applications, you need to transmit the field value back and forth due to the statelessness of the web. For example, you could simply put the value in a hidden field.
Note that this will expose the ID to the user.
You could also store certain session information on the server side and use a custom model binder so your app effectively knows what the user last did, but I would not recommend that.