@Html.Checkboxfor Works on GET, but not on POST

Asked

Viewed 223 times

2

I have the following model:

public class MEUViewModel
    {
        public List<Obj1> Obj1s { get; set; }
        public List<Obj2> Obj2s { get; set; }
        public int IDMeu { get; set; }
        public int IDMeu2 { get; set; }

        public bool Chk1 { get; set; }
        public bool Chk2 { get; set; }
        public bool Chk3 { get; set; }
        public bool Chk4 { get; set; }
    }

My POST method is basic signature:

[HttpPost]
        public ActionResult METODOPOST(MEUViewModel viewModel)
        {
          ...
        }

In my View I have the following Razor, one for each viewmodel bool:

@Html.CheckBoxFor(m => m.Chk1)

They are inside a BeginForm(), I’m not going to put all the code because by doing the submit he falls in the POST normally.

It generates the following HTML:

<input data-val="true" data-val-required="O campo Chk1 é obrigatório." id="Chk1" name="Chk1" type="checkbox" value="true">
<input name="Chk1" type="hidden" value="false">

When doing the POST all four bools come False, regardless of whether they are marked or not.

  • The two properties that have a list of objects comes with the normal bind.
  • Ids (ints) come with normal Ids.
  • If I put true in any of the four properties in the GET method it will generate HTML with checkbox marked.

UPDATE WITH TEMPORARY SOLUTION

After trying many things: - I put the HTML in my hand, according to the link Marconi sent - I tried Paul’s JS - I tried Warleson’s reply

and nothing working, thank you all.

What I did to work temporarily was change the signature of the method:

[HttpPost]
            public ActionResult METODOPOST(MEUViewModel viewModel, bool Chk1, bool Chk2, bool Chk3, bool Chk4)
            {

And I play the value of variables in the model. It’s not pretty, but it worked. I’ll leave the question open because I believe that a @Html.CheckBoxFor in such a basic structure should work more simply, perhaps someone will answer some better solution.

  • Look at this question she looks a lot like.

  • Guess what’s happening is : Going the value of the Hidden input

  • @Html.Editorfor(x => x.Chk1) Already tried this?

  • I’ve also tried @Paulohdsousa . Same result.

  • Before giving Submit, do this if ($('[name="Chk1"]:checked'). length > 0) $('[name="Chk1"]:Hidden'). val(true);

  • I did the JS, I sent an Alert to see if the value was changed, it was changed, but the controller is missing. @Paulohdsousa

Show 1 more comment

2 answers

1

Pass the value to the Controller

 @Html.CheckBoxFor(m => m.Chk1,new { Chk1  = true} )

By default it already passes as false, so when it is checked it sends True

  • This second parameter would not be to make the surrender? link. PS: I tried to do, it didn’t catch true no.

0

Some alternatives

@Html.CheckBoxFor(m => m.Chk1,new { Chk1  = true} )

or

 @Html.CheckBoxFor(model => model.Chk1, htmlAttributes: new { Chk1 = true })

or

@Html.CheckBoxFor(m => m.Chk1,true)

Controlle

[HttpPost]
public ActionResult METODOPOST(MEUViewModel viewModel)
            {

Structure of the Checkbox:

   public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool>> expression, object htmlAttributes);


  public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool>> expression, IDictionary<string, object> htmlAttributes);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.