I came across a problem with ambient form data, which I've tried to isolate into the following example.
View model:
public class ViewModel
{
public int Value { get; set; }
}
Controller:
public class TestController
{
public ViewResult Test1()
{
return(View(new ViewModel {Value = 1}));
}
[HttpPost]
public ViewResult Test2(ViewModel vm)
{
vm.Value = 2;
return(View(vm));
}
}
Test 1 view:
@model ViewModel
@using(Html.BeginForm("Test2", "Test"))
{
@Html.TextBoxFor(m => m.Value)
<button type="submit">Send</button>
}
Test 2 view:
@model ViewModel
@Model.Value
@Html.TextBoxFor(m => m.Value)
Submitting the form in view 1 gives the, at least to me, surprising result in view 2:
Seems like the posted value gets used in the TextBoxFor(). Anyone knows what's going on here and how to avoid it?