LabelFor
helper method render a label like this.
<label for="Increment">Increment</label>
This won't event show the value of your Increment property of the mdoel. If you want that, you may simply render it like
<p>@Model.Increment</p>
Or using the DisplayFor()
helper method.
@Html.DisplayFor(s=>s.Increment)
This will also simply render the value to your page. When you post the form, It is not going to post the current value in the page to the HttpPost action. For that you need a form field. It can be a textbox, hidden field etc..
@using(Html.BeginForm())
{
@Html.DisplayFor(s=>s.Increment)
@Html.HiddenFor(s => s.Increment)
<input type="submit"/>
}
But now also when you post the value and increment it in the action method and send it back, The input fields(hidden field) value won't be updated every time with the new value. Because the ModelStateDictionary
has some value for this property now, which is the value after the first update.This value will be used when we render the hidden field for Increment
property. That is the reason you are seeing 1
as the value and it never changes.
What you should do is explicitly remove this item from the ModelStateDictionary.
[HttpPost]
public ActionResult Index(IncrementModel IM)
{
IM.Increment++;
ModelState.Remove("Increment");
return View(IM);
}