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 =@Model.Back</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);
}
Tom el Safadi
  • 6,164
  • 5
  • 49
  • 102
  • 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. – stephen.vakil Sep 08 '16 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 – Tom el Safadi Sep 08 '16 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: http://stackoverflow.com/questions/9645479/view-not-updating-after-post – stephen.vakil Sep 08 '16 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 – Mathew Thompson Sep 08 '16 at 18:02
  • Yes now it works, thanks a lot! @stephen.vakil What is modelState.Clear() doing? Put is as the answer. I will accept it then. – Tom el Safadi Sep 08 '16 at 18:15
  • Don't use `ModelState.Clear`. That's like saying your car needs new brakes so let's rebuild your entire car. The problem here is that `ModelState`, *not* what's set on `Model` controls what the value of the input will be. While you set `model.Back = true`, you have a value in the post body for `Back` as `false` and that's what counts. – Chris Pratt Sep 08 '16 at 18:18
  • 1
    If you *must* do it this way, at least just use `ModelState.Remove("Back")`. – Chris Pratt Sep 08 '16 at 18:19
  • @ChrisPratt what is your preferred mechanism for this scenario? I can envision a rare but legitimate scenario where you might want to uncheck or re-check a field (e.g. Terms and conditions acceptance each time they submit). Better to avoid this behavior but sometimes it could be legit – stephen.vakil Sep 08 '16 at 18:23
  • Like I said, `ModelState.Remove`. Using `Clear` is the nuclear option. – Chris Pratt Sep 08 '16 at 18:27
  • And why do I even have those ModelState methods? I am passing the model to the view, so why doesn't it take the values from it?? I don't understand this... @ChrisPratt – Tom el Safadi Sep 08 '16 at 18:30
  • So when you said if you must do it this way, you mean, if you must set the value and have it come back? I inferred that you had an even better option. I probably misunderstood. – stephen.vakil Sep 08 '16 at 18:31
  • @Anokrize it's based on a built-in assumption that returning after a postback means that there was an error on the page, and the model might not have bound to all of the input field (e.g. if the data types were wrong of the input). More info [here](https://blogs.msdn.microsoft.com/simonince/2010/05/05/asp-net-mvcs-html-helpers-render-the-wrong-value/) – stephen.vakil Sep 08 '16 at 18:38
  • I meant simply that's it's a bad idea in general to override user input. Marketing types and stuff *love* to have that check box to subscribe to emails *always* checked, but users will assumed that their previous choice was persisted and not think to uncheck again (which is pretty much the point). However, that's bad UX and a fantastic way to piss off your users. The best method is to retain what the user did, but if you absolutely must override them, do it a granular way - don't just throw the baby out with the bath water. – Chris Pratt Sep 08 '16 at 18:51
  • This is just a help for the user if he didn't set the value... I am not pissing off the user @ChrisPratt – Tom el Safadi Sep 08 '16 at 18:54
  • That doesn't make sense to me. If it's a situation where if the user neglected to check it, you're going to check it for them, then there's no point in it being an option for the user in the first place. Not everything on your entity class has to have a corresponding form input. Just set the boolean based on your conditional and don't bother the user with the checkbox at all. Otherwise, let the user decide to leave it unchecked and don't mess with that choice. – Chris Pratt Sep 08 '16 at 18:58
  • See this is a buisness aplication, not for the "normal" user. There is a field and if this field changes from negative to positive then it should automatically set the the checkbox to true. This step should be done by the user, so it is just a help. But there are some cases where the user has to force the value to false, so it has to be editable by the user. Again, this is not a normal website or something, this is a buisness application @ChrisPratt – Tom el Safadi Sep 08 '16 at 19:03
  • It hardly matters whether it's a business or consumer app. The point still applies. If you're going to set it to true based on a conditional, there's no point in it being an option at all. Contrarily, if you want to allow the user to not select it, then you can't mess with that. How can the user uncheck it, if you're just going to set it to true anyways? – Chris Pratt Sep 08 '16 at 19:16
  • Just leave it by that ok? You don't have to say me how I have to programm my app... Still thanks for your answer @ChrisPratt – Tom el Safadi Sep 08 '16 at 19:17
  • It's called a discussion. We're all guilty of missing the forest for the trees sometimes. Here you have a logical inconsistency you need to deal with. It does neither of us any good to get ticked off because I pointed it out. – Chris Pratt Sep 08 '16 at 19:19
  • Haha okay mate @ChrisPratt – Tom el Safadi Sep 08 '16 at 19:20
  • You are a good programmer but I really need this to be editable and checking it automatically. If I could explain you the whole application then you would understand it... @ChrisPratt – Tom el Safadi Sep 08 '16 at 19:26
  • Just for the records. You don't need the id = "Back", Asp.NET MVC will put that by default. – Cat_Clan Sep 08 '16 at 23:07

0 Answers0