1

I have an Html.DropDownListFor, a textbox, and a model that contains a list "sourceWatchListParameters".

My end goal is, when an item is selected in the dropdownlist, populate the textbox with a property "DefaultParameter" for that particular sourceWatchList.

$(function() {
    $('select#WatchListDropDown').change(function () {
        var e = document.getElementById("#WatchListDropDown").selectedIndex.value;

        @foreach (var watchlist in Model.sourceWatchListParameters)
        {
            @:if (watchlist.WatchListId == e)
            {
                @: var def = document.getElementById("expDefault");
                @: def.value = watchlist.DefaultParameter;
            }
        }

    })
});

The function is called correctly, but I can't figure out the logic/syntax to locate the right sourceWatchListParameter and display its DefaultParameter. As it is now, I see no change in the textbox on selection. I'm sure there's a simpler way to rewrite this.

Thanks for any guidance

3
  • What is watchlist and why you're mixing JS and jQuery? Commented Aug 9, 2013 at 16:41
  • I'm mixing them i suppose because I'm confused Commented Aug 9, 2013 at 17:16
  • Okay, and what is "DefaultParameter"? Commented Aug 9, 2013 at 17:18

2 Answers 2

2

I suppose you have something like this:

@Html.DropDownListFor(
     m => m.WatchListDropDown, 
     Model.SourceListWatchListDropDown() ...)

Here you only have the value ('id') and the description, but you can do something like this to have the 3 values​​:

<select id="WatchListDropDown" name="WatchListDropDown">
@foreach (var wp in Model.sourceWatchListParameters)
{
   <option value="@wp.WatchListId" data-defparam="@wp.DefaultParameter">@wp.Description</option>
}
</select>

in this case, if you need get the default parameter, you only need to do this:

$(function() {
    $('#WatchListDropDown').bind('change', function () {
        var newValue = $(this).find('option:selected').data('defparam');
        $('#expDefault').val(newValue);;
    })
});

if your to render the page already have a value assigned, you can do the following:

   $('#WatchListDropDown').bind('change', function () {
       ...
   }).trigger('change');

Edit
Result for the solution of @Jonesy

Supposing that "Model.sourceWatchListParameters" has this value:

[
 {
    WatchListId = 1,
    Description = "Description 1"
 },
 {
    WatchListId = 3,
    Description = "Description 3"
 },
 {
    WatchListId = 3,
    Description = "Description 3"
 }
]

Your code would look like:

$('select#WatchListDropDown').change(function () {

    if ($('#WatchListDropDown').val() == "1")
        $("#expDefault").val("Description 1");
    }

    if ($('#WatchListDropDown').val() == "2")
        $("#expDefault").val("Description 2");
    }

    if ($('#WatchListDropDown').val() == "3")
        $("#expDefault").val("Description 3");
    }
})
Sign up to request clarification or add additional context in comments.

Comments

0

got it finally as such :

$('select#WatchListDropDown').change(function () {
        @foreach (var w in Model.sourceWatchListParameters)
        {
            @:if ($('#WatchListDropDown').val() == "@w.WatchListId.ToString()")
            @:{
                @: $("#expDefault").val("@w.Description");
            @:}
        }
    })

3 Comments

with this solution you have in your javascript code all items written and all 'if' for each item
I'm sorry what do you mean?
I'm respond you in my answerd.

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.