I have a view which is bound to a model. Inside this view I am displaying a checkbox like this:
@Html.CheckBoxFor(model => model.Back, new { id = "Back", @class = "target" })
<p style="color: white">Value [email protected]</p>
As you can see this CheckBox
is bound to the viewModel
.
Let's say that it is unchecked at first (value = false). Then the user makes a POST request
and inside this POST request
I updating the value from false
to true
. So normally the checkBox should be checked, though this isn't the case. Somehow it is not checked while the paragraph below displays true
... Why is that?
Thanks for your help!
Edit
Controller:
[HttpPost]
public ActionResult Details(Kunde model)
{
UserEntities userContext = new UserEntities();
KundeEntities KundeEntities = new KundeEntities();
var Kunde = KundeEntities.Kundes.Select(x => new { x.KdNr, x.Beratung }).Where(x => x.KdNr == model.KdNr).FirstOrDefault();
if(Kunde.Beratung == "Negativ" && model.Beratung == "Positiv")
{
model.Back = true;
}
return View(model);
}
Post
method that returns back a View without a redirect, it will re-use the values fromModelState
and disregard any changes you made in your model class unless you instruct it otherwise.ModelState
by default. UseModelState.Clear()
and see if it works. This is likely a duplicate of: stackoverflow.com/questions/9645479/…Back
property in your controller and because ofModelState
this won't actually propagate when you return your viewModelState.Remove("Back")
.