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.