2

I'm wondering why do we have to force @Value on field to send back different data after a post:

public ActionResult Index()
{
    var obj = new MyObject();
    obj.aString = "initial value";
    return View(obj);
}

[HttpPost]
public ActionResult Index(MyObject obj)
{
    obj.aString = "something else";
    return View(obj);
}

@using (Html.BeginForm())
{
    @Html.HiddenFor(model => model.aString)
    <input type="submit" />
}

If we do something like this, the hidden field is not "something else" after postback, but if I write this:

@Html.HiddenFor(model => model.aString, new { @Value = Model.aString})

Now aString will be "something else" after postback.

So why does it work like this ? It's not convenient at all.

Thank you!

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Sproulx
  • 314
  • 3
  • 13
  • 2
    See the answer here: http://stackoverflow.com/questions/4710447/asp-net-mvc-html-hiddenfor-with-wrong-value – Nikhil Vartak Nov 20 '15 at 03:25
  • 1
    Refer also [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) for an explanation of the behavior. `return View()` means return the current view. If you want to display a different model, then follow the normal PRG pattern and redirect back to the GET method. –  Nov 20 '15 at 04:33

0 Answers0