HitLikeAHammer
HitLikeAHammer

Reputation: 2695

ASP MVC 2 unedited fields come back null

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");
}

Upvotes: 1

Views: 155

Answers (2)

Mickel
Mickel

Reputation: 6696

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;

Upvotes: 0

mnemosyn
mnemosyn

Reputation: 46291

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.

Upvotes: 2

Related Questions