0

I want to use a variable from model:

Model.ActionId

and a varialble from javascript (selected index of dropdown)

$("#ddlMethod").val()

together in a if condition.

I have tried:

@if (Model.ActionID== 0 && $("#ddlMethod").val()== 2)
        alert("yes");

another:

if (@Model.ActionID== 0 && $("#ddlMethod").val()== 2)
        alert("yes");

assigning the values first:

var selectedMethod = $("#ddlMethod").val();
    var actionId = @Model.ActionID;
    if (actionId == 0 && selectedMethod == 2)
        alert("yes");

None of them worked. Help.

enter image description here

4
  • where you try use it? in separated script or inside page? you you put this scripts to yout page - last two variants should work Commented May 25, 2015 at 11:59
  • inside page only, bt no, its not working Commented May 25, 2015 at 12:04
  • it working, try fiddle: dotnetfiddle.net/sGcIho Commented May 25, 2015 at 12:04
  • @SurajS, see my update answer on why they didnt initially work Commented May 25, 2015 at 12:10

2 Answers 2

1

The simply way would be to put the model value in a hidden element like

<input type="hidden" value="@Model.ActionID" id="ActionID"/>

and then in your javascript

var selectedMethod = $("#ddlMethod").val();
var actionId = $("#ActionID").val();
if (actionId == 0 && selectedMethod == 2)
    alert("yes");

Update

if (@Model.ActionID== 0 && $("#ddlMethod").val()== 2)
        alert("yes");

will not work because you need to properly escape razor code when mixing with javascript by putting it in a code block

@{ ... } or @if, etc.

and putting the code itself in an escaped sequence

@: or the <text> tag.

Try

 @if (<text> '@Model.ActionID' </text> == 0 && $("#ddlMethod").val()== 2){
      <script>
                alert("yes");
      </script>
}
Sign up to request clarification or add additional context in comments.

5 Comments

are you try your last suggestion?
is it becasue of the single quotes in '@Model.ActionID' ?
nope, in this case parser don't know where used c# code, and where javascript
@Grundy, no, the last code snipped is in Razor view itself, not between <script></script>
1

if your script in the page so you have a few way for use server variable in your client code:

  1. use directly

    if (@Model.ActionID == 0 && $("#ddlMethod").val()== '2')
        alert("yes");
    

    in this case it rendered for Model.ActionID = 0 like

    if (0 == 0 && $("#ddlMethod").val()== '2')
        alert("yes");
    
  2. render this script tag only for needed action id like

    @if (Model.ActionID == 0){
        <script type="text/javascript">
            if( $("#ddlMethod").val()== '2')
                alert("yes");
        </script>
    }
    

    in this case this sctipt rendered and executed only when Model.ActionID == 0

If your script in separated file then you again have a few way:

use hidden field as suggest @AmmarCSE, or save Model.ActionID to global varibale in your page

var modelActionId = @Model.ActionID

and use it in script

if (modelActionId == 0 && $("#ddlMethod").val()== '2')
    alert("yes");

NOTE: method val, in your case, return string rather than number

3 Comments

Thank you for explanation, but the first one does not work. gives error as "incorrect syntax.". '@Model.ActinId' works.. with single quotes.
I checked the fiddle, don't know whats the difference.. attched the screen shot.. ;)
@SurajS, can you provide a code around this? what version mvc you use?

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.