i have a dropdown list, when it changes. i want the selected value to go into the controller using ajax
$(document).ready(function () {
$("#otherCatches").change(function () {
$.ajax({
url: "Clients/DDL",
type: "POST",
data: {
name: $('#othercatches').val()
},
success: function (result) {
alert(result)
}
});
return false;
});
});
<select id ="otherCatches">
@foreach (var item in Model.Sample)
{
<option>
@item.SampleName
</option>
}
</select>
It is not hitting the controller
[HttpPost]
public virtual ActionResult DDL(int name)
{
//do method here
}
In your View code, you are not setting the
valueattribute of theoption. So$('#othercatches').val()will give you undefined.Use the
DropDownList/DropDownListForHTML Helper method to render a SELECT element.Use Strongly Type Views. Ex : If your view is for Creating a DDL, You will define a viewmodel like this
Now in our
GETaction,We will create an object of this ViewModel and send that to the view.Now our view (
CreateDDL.cshtml),which is strongly typed to ourClientDDLclass will look like thisNever hardcode paths to action method like that. Use the URL Helper methods wherever possible.
Not sure why you have a
virtualmethod ? It should be simple as this