[英]Ajax postbacks with cascading ListBox in ASP.NET MVC3
首先,我是一个ASP.NET MVC新手。 这是我使用ASP.NET MVC的第一个项目,因此我仍在学习。 在过去的两年中,我的工作背景主要是WPF和XAML。
所以这是我的问题:我有三个层叠的ListBoxes。 第二个列表框数据依赖于第一个,而第三个依赖于第二个。 我想使用Ajax刷新来填充每个列表中的数据。
这是我的Index.cshtml:
@model WebApplication.Models.DevelopmentModel
<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
</head>
<body class="body" scroll="auto">
<div class="page">
<div class="content">
<div id="lists">
@Html.Partial("DevelopmentListsView", Model)
</div>
</div>
</div>
</body>
</html>
我的DevelopmentListsView.cshtml看起来像这样:
@model WebApplication.Models.DevelopmentModel
@using (Ajax.BeginForm("Index", "Development", new AjaxOptions() { UpdateTargetId = "lists" } ))
{
@Html.ListBoxFor(m => m.SelectedApplication, new SelectList(ViewBag.Applications), new { onchange = "this.form.submit();" })
@Html.ListBoxFor(m => m.SelectedVersion, new SelectList(ViewBag.Versions), new { onchange = "this.form.submit();" })
@Html.ListBoxFor(m => m.SelectedFlow, new SelectList(ViewBag.Flows) )
}
我的模型如下:
public class DevelopmentModel
{
public string SelectedApplication { get; set; }
public string SelectedVersion { get; set; }
public string SelectedFlow { get; set; }
}
我的控制器看起来像这样:
public class DevelopmentController : Controller
{
//
// GET: /Development/
public ActionResult Index()
{
FillViewBag();
return View(new DevelopmentModel());
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(DevelopmentModel model)
{
FillViewBag(model);
return PartialView("DevelopmentListsView", model);
}
private void FillViewBag(DevelopmentModel model = null)
{
//Magic to get all three lists dependent on the available data in the model:
ViewBag.Applications = applications;
ViewBag.Versions = versions;
ViewBag.Flows = flows;
}
}
现在,我想使用Ajax回调来检索数据,因此它不会每次都刷新,但是当我单击列表框项之一时,该页面仅在此之后显示DevelopmentListsView视图,而不刷新任何内容。
有人能告诉我我做错了什么吗?
谢谢你的期待!
想通了我自己的问题:
我有两个错误:
我错过了Index.cshtml中包含的jquery脚本:
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
而且我使用了错误的提交(应该是jQuery提交):
$(this.form).submit()
提交放置在我的模型中
@model WebApplication.Models.DevelopmentModel
@using (Ajax.BeginForm("Index", "Development", new AjaxOptions() { UpdateTargetId = "lists" } ))
{
@Html.ListBoxFor(m => m.SelectedApplication, new SelectList(ViewBag.Applications), new { onchange = "$(this.form).submit()" })
@Html.ListBoxFor(m => m.SelectedVersion, new SelectList(ViewBag.Versions), new { onchange = "$(this.form).submit()" })
@Html.ListBoxFor(m => m.SelectedFlow, new SelectList(ViewBag.Flows) )
}
希望有一天能对某人有所帮助;)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.