2

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 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(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.

balexandre
  • 73,608
  • 45
  • 233
  • 342
  • Not the answer but you might want to look at the PRG pattern rather than posting back to the same action method: e.g. http://www.timbarcz.com/blog/PRGPatternInTheASPNETMVCFramework.aspx. Also, are you missing an [HttpGet] attribute on the first line? – Ian Mercer Nov 10 '10 at 04:13
  • @Hightechrider `[HttpGet]` is there by default, no need to append anything :) – balexandre Nov 10 '10 at 08:18

2 Answers2

2

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.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • you're like an ASP.NET MVC Library :) Thank you for the help, but it's hard to find something when we don't know the correct keywords to get from, I tried over and over on Google with "model binding passing model values" even tried for the 3rd time Bing... nothing come up so precise as this. once again, thank you so much for the trouble! – balexandre Nov 10 '10 at 08:16
  • @balexandre, I agree that in this case the default behavior of HTML helpers is counter intuitive. – Darin Dimitrov Nov 10 '10 at 08:22
  • And I always forget and waste a couple of hours before a very quiet bell starts to ring in the back of my mind that something like this is the case, then I have to try and find some reference to it online to bring it all back to mind. As has just happened again. Thanks! – Philip Stratford Nov 11 '21 at 14:01
0

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); 
} 
Luke101
  • 63,072
  • 85
  • 231
  • 359