1

So my view receives a model. And I want to, in case the ID property of the model is -1, make the DDL select the default value "[Select]" that is in there.

DDL:

@Html.DropDownListFor(cModel => cModel.QueueMonitorConfigTypeName, Enum.GetValues(typeof(BPM.Website.Models.PathType)).Cast<BPM.Website.Models.PathType>().Select(v => new SelectListItem
    {
        Text = v.ToString(),
        Value = v.ToString()
    }), "[Select]", new { id = "ddlConfigTypeName" })

And the js:

<script type="text/javascript">
    $(function () {
        var model = @Model.QueueMonitorConfigurationsID
        if (model = -1)
        {
            $('#ddlConfigTypeName').select("[Select]")
        }
        else{}
    });
</script>

But for some reason , the intellisense of the js is telling me it is wrong. The problem comes when I try to do a compare in the if. I've tried with '=', '==', '===' and nothing works

It just keep saying syntax error. I guess it has some relation with adding the @Model tag but cant resolve it. Any guesses?

Thanks

EDIT and if I change it with this:

<script type="text/javascript">
    $(function () {
        if (@Model.QueueMonitorConfigurationsIDel == -1)
        {
            $('#ddlConfigTypeName').select("[Select]")
        }
        else{}
    });
</script>

It still says the syntax error is in the ==

0

2 Answers 2

1

It thinks you're doing an assignment (with just one = ) in this first one

$(function () {
    var model = @Model.QueueMonitorConfigurationsID
    if (model = -1)
    {
        $('#ddlConfigTypeName').select("[Select]")
    }
    else{}
});

I replicated your situation, and this works:

   <script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
   <script type="text/javascript">
$(function () {

    var model = @Model.QueueMonitorConfigurationsID;
    if (model == -1){

        alert('got it');
    }
    else{

   alert(model);
  }
});
</script>

If this still doesn't work for you, can you check the value of the variable "model"? what is it returning?

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

3 Comments

Well, I tried with =, == and it says syntax error. Seems like my intellisense says wrong things because it works. Does your intellisense say syntax error?
Yes it does. Right on the last "});". But this is due to the use of "@Model". You can prove it by replacing "@Model.QueueMonitorConfigurationsID" with just a number, the error will go away. Anyway it won't affect your outcome.
Thank you :) so sad that intellisense doesnt read that properly because is confusing
0
  1. There is a ";" missing in the JS behind var model = @Model.QueueMonitorConfigurationsID
  2. Change if (model = -1) to if (model == -1)

1 Comment

Ignore it, that's a bug in Intellisense. It runs.

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.