javascript

Calling jQuery code in razor block of code  #angularjs #reactjs

  • The parameter basically states whether the “remember me” checkbox has been checked on the form or not.
  • you should set variable value=”true”, i ve updated the answer.
  • If you want the value of the checkbox on post, then just bind it to something your action accepts.
  • To actually alter the form action without posting first, you’d have to handle the click event of the checkbox and manually change it via JavaScript.
  • Public ActionResult Login(bool Checked, LoginViewModel model) { // To have the checked value now…

Note that besides passing the entire model into my Login action I’m trying to pass an optional parameter named “Checked”. This parameter basically states whether the “remember me” checkbox has been checked on the form or not.

@ng_real_ninja: Calling jQuery code in razor block of code #angularjs #reactjs

I have a problem where I need to call jQuery code to fetch a value from an HTML element like following:

Note that besides passing the entire model into my Login action I’m trying to pass an optional parameter named “Checked”. This parameter basically states whether the “remember me” checkbox has been checked on the form or not.

The checkbox itself is not the one that .NET uses by default like:

But instead a regular checkbox like this:

How can I fetch this checkbox’s value when passing it as an extra parameter besides my model?

So that my method in the end would look like:

P.S. And I can’t use Html.CheckboxFor for some reasons, but I don’t wanna get too much into details since the question wouldn’t make sense then (maybe it doensn’t even now I’m not sure if this is doable what I’m trying to achieve).

Can someone help me out?

You can get checkbox value in Controller using Checkbox Field name like this :

if you want to send any variable, you should set name for field, and on serverside you can call it with name

on server side

You can’t use JavaScript within a Razor block, because they run at entirely different times. The Razor code runs server-side, long before the response is ever sent to the client. JavaScript runs client-side, only after the server has sent its response to the client, and Razor has already done its work.

. That’s why none of the other answers help you. The user would have to interact with the checkbox and submit the form first, and then the checkbox would have that user-set value within the post action that handles the form. If you returned to the form, because of an error, then you could utilize the checkbox value.

To actually alter the form action without posting first, you’d have to handle the click event of the checkbox and manually change it via JavaScript. However, that’s extremely brittle. If you want the value of the checkbox on post, then just bind it to something your action accepts. For example:

Then:

javascript

You might also like More from author

Comments are closed, but trackbacks and pingbacks are open.