ASP.NET MVC3.0 DropDownList的二级联动问题,都是从数据库读取的,全部都写好了,但是没有出现联动效果? 50
cshtml部分代码:<divclass="editor-label">@Html.Label("房间类型:")@Html.DropDownList("TypeID",V...
cshtml部分代码:
<div class="editor-label">
@Html.Label("房间类型: ")
@Html.DropDownList("TypeID", ViewBag.Types as IEnumerable<SelectListItem>, "--请选择--")
@Html.Label("房间号: ")
@Html.DropDownList("RoomID", ViewBag.Rooms as IEnumerable<SelectListItem>, "--请选择--")
</div>
js代码:
<script type="text/javascript">// <![CDATA[
$(document).ready(function () {
$("#TypeID").change(function () {
var url = "/Guest/GetRoomNumber/" + $("#TypeID").val() + "/"; //规则是控制器/方法/参数
$.getJSON(url, function (data) {
$('#RoomID').html('');
$.each(data, function (i, item) {
$('#RoomID').append($("<option></option>").val(item.RoomID).html(item.Number));
});
});
});
});// ]]>
</script>
Controller的代码:
public class GuestController : Controller
{
HotelManageEntities db = new HotelManageEntities();
public ActionResult Create()
{
ViewBag.Types = db.RoomType.ToList().Select(c => new SelectListItem { Value = c.TypeID.ToString(), Text = c.TypeName });
ViewBag.Rooms = db.Room.ToList().Select(c => new SelectListItem { Value = c.RoomID.ToString(), Text = c.Number });
}
public JsonResult GetRoomNumber(int id)
{
HotelManageEntities context = new Models.HotelManageEntities();
var RoomNumber = from a in context.Room where a.TypeID == id select a;
return Json(RoomNumber.ToList(), JsonRequestBehavior.AllowGet);
}
} 展开
<div class="editor-label">
@Html.Label("房间类型: ")
@Html.DropDownList("TypeID", ViewBag.Types as IEnumerable<SelectListItem>, "--请选择--")
@Html.Label("房间号: ")
@Html.DropDownList("RoomID", ViewBag.Rooms as IEnumerable<SelectListItem>, "--请选择--")
</div>
js代码:
<script type="text/javascript">// <![CDATA[
$(document).ready(function () {
$("#TypeID").change(function () {
var url = "/Guest/GetRoomNumber/" + $("#TypeID").val() + "/"; //规则是控制器/方法/参数
$.getJSON(url, function (data) {
$('#RoomID').html('');
$.each(data, function (i, item) {
$('#RoomID').append($("<option></option>").val(item.RoomID).html(item.Number));
});
});
});
});// ]]>
</script>
Controller的代码:
public class GuestController : Controller
{
HotelManageEntities db = new HotelManageEntities();
public ActionResult Create()
{
ViewBag.Types = db.RoomType.ToList().Select(c => new SelectListItem { Value = c.TypeID.ToString(), Text = c.TypeName });
ViewBag.Rooms = db.Room.ToList().Select(c => new SelectListItem { Value = c.RoomID.ToString(), Text = c.Number });
}
public JsonResult GetRoomNumber(int id)
{
HotelManageEntities context = new Models.HotelManageEntities();
var RoomNumber = from a in context.Room where a.TypeID == id select a;
return Json(RoomNumber.ToList(), JsonRequestBehavior.AllowGet);
}
} 展开
展开全部
二级联动,细心点不难
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你检查一下是不是js没有触发?
追问
怎么检查呢?教一下
追答
在js里面写alert,看出触发了没有,或者用vs/ie加断点
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用js写啊 ajax
追问
能示范一下怎么写吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询