ASP.NET MVC 4: Changing the Value of a Hidden fiel

2019-06-16 04:14发布

问题:

I have a hidden boolean field :

@Html.HiddenFor(x => x.IsTurkey)

In jQuery script I try to change it:

$("@Html.IdFor(x => x.IsTurkey)").val("False");

But on the post back IsTurkey is not changed:

    HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Search(TurkeyModel model)
    { ...}

Using jQuery as above, how do I change the value of a hidden boolean field in MVC 4 ?

回答1:

Make sure you include a leading # in the Jquery selector, as the MVC HtmlHelper does not output it.



回答2:

I'm not sure your exact code for IdFor, but you can simply do:

$("#IsTurkey").val(false);