9

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
  • 1
    I might be mistaken but I think you'd want to do $(Html.IdFor(x => x.IsTurkey)), also try not to mix server-side and front-end coding. Can you not get the ID of it any other way? Commented Mar 27, 2013 at 17:25
  • In MVC 4, $("#@Html.IdFor(x => x.IsTurkey)") is the correct way to create the javascript dynamically. Commented Mar 27, 2013 at 17:28

2 Answers 2

11

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

Sign up to request clarification or add additional context in comments.

1 Comment

That did it, I will mark this as the answer once the timer lets me.
5

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

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

1 Comment

@BahaiResearch.com Hmm, while debugging your post, what value is in the Request.Form for IsTurkey?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.