MVC3中的Html.DropDownListFor用AJAX怎么写级联???

dsmetty 2012-03-29 11:14:50
MVC3中的Html.DropDownListFor用AJAX怎么写级联???我以前都是用Webforms的触发事件写的,现在换MVC3,不会做了。

<div class="row">
@Html.LabelFor(model => model.Category)
@Html.DropDownListFor(model => model.Category, taomei.BLL.MerchantManager.GetMerCategorysIL())
@Html.DropDownListFor(model => model.Category, taomei.BLL.MerchantManager.GetSecondMerCategorysIL())
@Html.ValidationMessageFor(model => model.Category)
</div>


这段代码中有两个DropDownListFor,我想让这两个控件实现级联,问了朋友说只能用ajax,但我实在不知如何下手!
以前用ajax只是用于无刷新
...全文
652 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lonkong 2014-07-04
  • 打赏
  • 举报
回复
同问。。DropDownListFor 如何实现的
灬隨風灬 2013-12-20
  • 打赏
  • 举报
回复
LZ你好,我现在也在做级联的,如果要直接用DropDownListFor做,可以吗?
dsmetty 2012-04-01
  • 打赏
  • 举报
回复
问题已经解决了
Jquery代码:
$(document).ready(function () {
//第一级
$("#Cate1").change(function () {
var _val = $(this).val();
$.ajax({
type: "get", //要执行的Action Method,如:get,post
url: "/Admin/Merchant/GetCategory",
datetype: "html", //返回的文本类型
data: "cid=" + _val, //如: ID=5
success: function (res) { //调用成功时执行
eval("var res=" + res); //将字符串转换为对象
var target = $("#Cate2");
var targetHtml = ""; //目标DropDownList内容初始为空
for (var i = 0; i < res.length; i++)
targetHtml += "<option value=\"" + res[i].CID + "\">" + res[i].CName + "</option>";
target.html(targetHtml);
},
complete: function (XMLHttpRequest, textStatus) { },
error: function () { alert("服务器超时!"); }
})
});
});

页面代码:
@Html.DropDownList("Category1", taomei.BLL.MerchantManager.GetMerCategorysIL(), Model == null ? null : Model.Category.Substring(0,Model.Category.Length-2), new Dictionary<string, object> { {"ID","Cate1"}})
@Html.DropDownList("Category", taomei.BLL.MerchantManager.GetSecondMerCategorysIL(), Model == null ? null : Model.Category, new Dictionary<string, object> { {"ID","Cate2"}})

52,782

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧