所以我正在构建一个简单的 mvc4 应用程序,我已经创建了用于创建数据库的所有基本模型,从这些类中我可以自然地创建具有匹配视图的基本控制器。
现在我的问题:我有基本的 create actionresult + 视图,在这个视图中,我希望用户能够从下拉列表中选择一些值,这将使新对象的创建更简单。
如果我想使用这些下拉菜单(它们相互过滤(所以首先用户必须指定一个大陆,然后是国家只显示来自该大陆的国家,并且在他指定一个国家之后,区域下拉列表会更新:) )) 基本视图的提交总是被自动调用。
所以让下拉列表自己更新不是问题:s 是创建的表单会在下拉列表更新时自动验证
这是下拉菜单相互过滤的控制器
//
// GET: /FederationCenter/Create
public ActionResult Create(string searchRegion, string searchCountry, string searchContinent)
{
var countries = from c in db.Countries select c;
if (!String.IsNullOrEmpty(searchContinent))
{
Continent searchContinentEnumValue = (Continent)Enum.Parse(typeof(Continent), searchContinent);
countries = from c in db.Countries where ((int)c.Continent).Equals((int)searchContinentEnumValue) select c;
}
var regions = from r in db.Regions where r.Country.Name.Equals(searchCountry) select r;
ViewBag.searchContinent = new SelectList(Enum.GetNames(typeof(SchoolCup.Domain.Continent)));
ViewBag.searchCountry = new SelectList(countries, "Name", "Name");
ViewBag.searchRegion = new SelectList(regions, "Name", "Name");
return View();
}
//
// POST: /FederationCenter/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(NSSF nssf, string searchRegion, string searchCountry, string searchContinent)
{
var countries = from c in db.Countries select c;
if (!String.IsNullOrEmpty(searchContinent))
{
Continent searchContinentEnumValue = (Continent)Enum.Parse(typeof(Continent), searchContinent);
countries = from c in db.Countries where ((int)c.Continent).Equals((int)searchContinentEnumValue) select c;
}
var regions = from r in db.Regions where r.Country.Name.Equals(searchCountry) select r;
ViewBag.searchContinent = new SelectList(Enum.GetNames(typeof(SchoolCup.Domain.Continent)));
ViewBag.searchCountry = new SelectList(countries, "Name", "Name");
ViewBag.searchRegion = new SelectList(regions, "Name", "Name");
if (ModelState.IsValid)
{
var naam = Request["searchRegion"];
Region creatie = (from c in db.Regions where c.Name.Equals(naam) select c).SingleOrDefault();
nssf.ISFId = 1;
nssf.RegionId = creatie.RegionId;
db.NSSFs.Add(nssf);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(nssf);
}
这是我的观点
@model SchoolCup.Domain.POCO.NSSF
@{
ViewBag.Title = "Create";
}
<h2>Create NSSF</h2>
<div>
@using (Html.BeginForm(null, null, FormMethod.Post, new { name = "form" }))
{
@Html.AntiForgeryToken()
@Html.DropDownList("searchContinent", null, "-- All continents --", new { onchange = "sendForm()" })
@Html.DropDownList("searchCountry", null, "-- All countries --", new { onchange = "sendForm()" })
@Html.DropDownList("searchRegion", null, "-- All regions --", new { onchange = "sendForm()" })
<>
<input type="submit" name= value="Search" />
}
</div>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>NSSF</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
更多输入
</fieldset>
<p>
<input type="submit" value="Create" />
@Html.ActionLink("Back to List", "Index", null, new { @class = "button" })
</p>
}
@section Scripts {
<script type="text/javascript">
function sendForm() {
document.form.submit()
}
</script>
}
我一直在寻找至少一天,我不知道如何解决这个问题
关于亚历山大