C# 下拉列表MVC 3中选定的索引更改事件

C# 下拉列表MVC 3中选定的索引更改事件,c#,asp.net-mvc-3,razor,C#,Asp.net Mvc 3,Razor,如何编写MVC 3 Razor web应用程序选择的下拉列表索引更改事件您必须使用ajax编写Javascript,如前面的答案所述。但是,它也可以简单到将表单发布回同一操作,并捕获值以再次重新显示修改后的视图 $('#myDropdown').change(function(){ $('#myForm').submit(); }) $(文档).ready(函数(){ $(“#国家”)。更改(功能(){ 如果($(“#country”).val()!=“0”){ var选项={}; o

如何编写MVC 3 Razor web应用程序选择的下拉列表索引更改事件

您必须使用ajax编写Javascript,如前面的答案所述。但是,它也可以简单到将表单发布回同一操作,并捕获值以再次重新显示修改后的视图

$('#myDropdown').change(function(){
    $('#myForm').submit();
})
$(文档).ready(函数(){
$(“#国家”)。更改(功能(){
如果($(“#country”).val()!=“0”){
var选项={};
options.url=“/Common/GetStates”;
options.type=“POST”;
options.data=JSON.stringify({country:$(“#country”).val()});
options.dataType=“json”;
options.contentType=“application/json”;
options.success=功能(状态){
//警报(状态[i]。状态);
$(“#state”).empty();
对于(变量i=0;i
鉴于

选择
控制
public JsonResult GetStates(字符串国家){
int cntry=转换为32(国家/地区);
List states=db.states.Where(i=>i.Countryid==cntry.ToList();
返回Json(状态);}
此问题可能会帮助您:
$(document).ready(function () {
$("#country").change(function () {
if ($("#country").val() != "0") {
var options = {};
options.url = "/Common/GetStates";
options.type = "POST";
options.data = JSON.stringify({ country: $("#country").val() });
options.dataType = "json";
options.contentType = "application/json";
options.success = function (states) {
//alert(states[i].State);
$("#state").empty();
for (var i = 0; i < states.length; i++) {
$("#state").append("<option>" + states[i].State1 + "</option>");}}
options.error = function () { alert("Error retrieving states!"); };
$.ajax(options);}
else {$("#state").empty();
}});
});
<select id="state">
<option value="0">select</option>
</select>
public JsonResult GetStates(string country){
int cntry = Convert.ToInt32(country);
List<State> states = db.States.Where(i => i.Countryid == cntry).ToList();
return Json(states);}