0

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);
}
22
  • Post your controller code. Generally, in a Post method that returns back a View without a redirect, it will re-use the values from ModelState and disregard any changes you made in your model class unless you instruct it otherwise.
    – steve v
    Commented Sep 8, 2016 at 17:57
  • edited it. But thats not the problem I think. Because all other values arechanging except the checkbox. This might be a bug... @stephen.vakil Commented Sep 8, 2016 at 17:59
  • It seems very likely that it is in fact the problem. You are setting a value on the model but it's going to read from ModelState by default. Use ModelState.Clear() and see if it works. This is likely a duplicate of: stackoverflow.com/questions/9645479/…
    – steve v
    Commented Sep 8, 2016 at 18:02
  • @stephen.vakil is right, you're only actually updating the Back property in your controller and because of ModelState this won't actually propagate when you return your view Commented Sep 8, 2016 at 18:02
  • 1
    If you must do it this way, at least just use ModelState.Remove("Back"). Commented Sep 8, 2016 at 18:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.