C# 尝试在asp.net mvc中使用级联下拉列表
我试图根据单独的dropdownlist的值更改dropdownlist中的项目。有人告诉我,我想要的是一个层叠的下拉列表。我遵循了asp.NETMVC4的教程,下面是我的代码,但它不起作用,我看不出有任何问题 以下是当用户更改父dropdownlist的值时运行的jquery/ajax代码:C# 尝试在asp.net mvc中使用级联下拉列表,c#,jquery,ajax,asp.net-mvc-4,C#,Jquery,Ajax,Asp.net Mvc 4,我试图根据单独的dropdownlist的值更改dropdownlist中的项目。有人告诉我,我想要的是一个层叠的下拉列表。我遵循了asp.NETMVC4的教程,下面是我的代码,但它不起作用,我看不出有任何问题 以下是当用户更改父dropdownlist的值时运行的jquery/ajax代码: $('#paperTypeJList').change(function () { var modelData = { paperType: $("
$('#paperTypeJList').change(function () {
var modelData = {
paperType: $("#paperTypeJList").text,
urgency: $("#urgencyJList").val(),
numOfPages: $("#numOfPagesJList").val()
};
var typeValue = $("#paperTypeJList").val();
var url = @Url.Action("UrgencyList", "Home", typeValue);
$.getJSON(url, function(data) {
var items = "<option>Select the Urgency</option>";
$.each(data, function(i, urgency) {
items += "<option value='" + urgency.Value + "'>" + urgency.Text + "</option>";
});
$("#urgencyJList").html(items);
});
$.ajax({
url: '@Url.Action("getNewPrice","Home")',
type: "POST",
data: modelData,
cache: false,
async: true,
success: function(response){
document.getElementById('priceLabel').innerHTML = response;
}
});
});
$(function () {
var url = '@Url.Action("PaperTypeList","Home")';
$.getJSON(url, function (data) {
var items = "<option>Select the Type of Paper</option>";
$.each(data, function (i, papertype) {
items += "<option value='" + papertype.Value + "'>" + papertype.Text + "</option>";
});
$('#paperTypeJList').html(items);
});
最后但并非最不重要的一点是,以下是我的jquery代码,它为父dropdownlist创建了项:
$('#paperTypeJList').change(function () {
var modelData = {
paperType: $("#paperTypeJList").text,
urgency: $("#urgencyJList").val(),
numOfPages: $("#numOfPagesJList").val()
};
var typeValue = $("#paperTypeJList").val();
var url = @Url.Action("UrgencyList", "Home", typeValue);
$.getJSON(url, function(data) {
var items = "<option>Select the Urgency</option>";
$.each(data, function(i, urgency) {
items += "<option value='" + urgency.Value + "'>" + urgency.Text + "</option>";
});
$("#urgencyJList").html(items);
});
$.ajax({
url: '@Url.Action("getNewPrice","Home")',
type: "POST",
data: modelData,
cache: false,
async: true,
success: function(response){
document.getElementById('priceLabel').innerHTML = response;
}
});
});
$(function () {
var url = '@Url.Action("PaperTypeList","Home")';
$.getJSON(url, function (data) {
var items = "<option>Select the Type of Paper</option>";
$.each(data, function (i, papertype) {
items += "<option value='" + papertype.Value + "'>" + papertype.Text + "</option>";
});
$('#paperTypeJList').html(items);
});
$(函数(){
var url='@url.Action(“纸质列表”、“主页”);
$.getJSON(url、函数(数据){
var items=“选择纸张类型”;
$。每个(数据、功能(i、纸张类型){
项目+=“”+纸张类型。文本+“”;
});
$('#paperTypeJList').html(项目);
});
})) 将操作列表的参数名称重命名为
public ActionResult UrgencyList(string id)
MVC无法解析名称“deadlineValue”,您必须映射一条新路线,但我不建议这样做
要了解有关路线的更多信息,请打开App_Start/RouteConfig.cs文件。在这里,您将看到MVC默认映射了“id”
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",//Id is mapped by default
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
我将参数名更改为id,现在得到以下行的编译错误:var typeValue=$(“#paperTypeJList”).val();var url=@url.Action(“UrgencyList”、“Home”、typeValue);这意味着typeValue在当前上下文中不存在