1

I'm using DropDownListFor in my view:

<%: Html.DropDownListFor(model => model.Type, 
                        new SelectList(ComponentsFactory.GetTypeList().Select(a => new { Name = Regex.Replace(a, @"^.*\.", ""), Value = a }).OrderBy(a => a.Name),
                            "Value", "Name", Model.Type), "Select . . .", null) %> 

I also have a JQuery function in a .js scripts file that is already included in my header and everything:

function GetRequiredFields(type) 
{
...//do JQuery AJAX stuff
}

I want to call "GetRequiredFields" with parameter "type" as the value of the selected item on select, and use it to update a 'div' named "RequiredFields". How can I do this? Do I need to use something other than Html.DropDownListFor?

2 Answers 2

1
$(function(){
   $('#Type').change(function(){GetRequiredFields( $(this).val() );});
});
Sign up to request clarification or add additional context in comments.

Comments

0

try this

<%: Html.DropDownListFor(model => model.Type, 
                    new SelectList(ComponentsFactory.GetTypeList().Select(a => new { Name = Regex.Replace(a, @"^.*\.", ""), Value = a }).OrderBy(a => a.Name),
                        "Value", "Name", Model.Type), "Select . . .", new { @onchange = "GetRequiredFields(this);" }) %> 

The above new { @onchange = "GetRequiredFields(this);" } will create a new attribute onchange for the generated <select>.

and in your GetRequiredFields() add this

function GetRequiredFields(type) 
{
    var selectedValue = $(type).val();
    ...//do JQuery AJAX stuff
}

Comments

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.