首先,我是一个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视图,不会刷新任何内容。
谁能告诉我我哪里做错了?
感谢您的关注!
发布于 2011-08-08 12:12:14
弄清楚我自己的问题:
我有两个错误:
我错过了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) )
}
希望有一天这对某人有帮助;)。
https://stackoverflow.com/questions/6981498
复制