开发者

How can I pass a modified Model back into the view?

Imagine a simple Action with a post

public ActionResult Unsubscribe(string contentId)
{
    // get db content and translate it to the UnsubscribeViewModel
    UnsubscribeViewModel model = 
        myRepository.FindUnsubscribeContentById(contentId).ToUnsubscribeViewModel();

    // pass model into the view
    return View(model);
}

[HttpPost]
public ActionResult Unsubscribe(UnsubscribeViewModel model)
{
    // let's co开发者_JAVA百科nvert into DalModel
    UnsubscribeModel m = model.ToUnsubscribeModel();

    // let's save into the db
    myRepository.UpdateUnsubscribe(m);

    // because I'm using 2 models, let's get the new ID 
    // and append to he View model
    model.FieldId = m.unsubscribe_id;

    // let's return the new model to the View
    return View(model);
}

my current problem is, even if I do a breakpoint on the return View(model) line, I DO HAVE the model.FieldId correctly assigned, but in the HTML Page, I have the original value (in this case a 0 because there was no prior ID, it's a new record)

in the View I tried:

<%: Html.HiddenFor(x => x.FieldId) %>

and

<%: Html.Hidden("FieldId", Model.FieldId) %>

and they still have "0" as the value, like

<input type="hidden" value="0" name="FieldId" id="FieldId">

if I refresh the page, my Action fetch the new data and the value is changed to the correct id. But if I use RedirectToAction("Unsubscribe") I will loose the data and can't pass a Success/Error message trough ViewData, I have to use RedirectToAction("Unsubscribe", new { something = msg }) and I don't want this way.

Why is the View loading the original value instead the newly updated value from the Model?

Thank you.


The problem is not in passing modified model back to the view. This happens correctly. Your issue is with the way HTML helpers work. That's their normal behavior is by design. When binding the value of the input field they first look at POSTed values and after that in ViewData and view model. This means that you cannot modify the POSTed values in your controller action. As a workaround:

<input type="hidden" value="<%= Model.FieldId %>" name="FieldId" id="FieldId" />

I have answered this many times before and continue to answer it hoping that people will finally start noticing it.


You can try passing it to another controller. Try this and see if it works. I have created an additional controller. You will need to create a new view to make this work

[HttpPost] 
public ActionResult Unsubscribe(UnsubscribeViewModel model) 
{ 
    // let's convert into DalModel 
    UnsubscribeModel m = model.ToUnsubscribeModel(); 

    // let's save into the db 
    myRepository.UpdateUnsubscribe(m); 

    // because I'm using 2 models, let's get the new ID  
    // and append to he View model 
    model.FieldId = m.unsubscribe_id; 

    // let's return the new model to the View 
    return View("UnsubscribeResult", model); 
} 


public ActionResult UnsubscribeResult(UnsubscribeViewModel model) 
{ 
     return View(model); 
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜