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

2019-06-16 04:34发布

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 ?

2条回答
Emotional °昔
2楼-- · 2019-06-16 05:13

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

查看更多
乱世女痞
3楼-- · 2019-06-16 05:16

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

$("#IsTurkey").val(false);
查看更多
登录 后发表回答