tags:

views:

38

answers:

2

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");
}
+1  A: 

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.

<input type="hidden" name="QuoteOptionID" value="...." />

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.

mnemosyn
I thought the data would somehow come back but I guess not. This has prompted me to switch to surogate keys (guids) for security.
Loki Stormbringer
A: 

If you write it like this you will get the id separately (as it only exists in the querystring and not in your form).

[HttpPost]
public ActionResult Edit(QuoteOption quoteOptionToUpdate, Long id)

In the edit-method you can then assign the id to the quoteOptionToUpdate:

quoteOptionToUpdate.Id = id;
Mickel