0
我有我的网站的仪表板控制器和回报的类别为列表的操作来查看和另一个行动,创造新的类别结合创建和索引视图在Asp.Net MVC一个View 5
我的操作:
public ActionResult Categories() {
return View(db.Categories.OrderBy(Categories => Categories.Name).ToList());
}
[HttpPost]
public RedirectToRouteResult NewCategory(string name, string address, string parentId) {
if (ModelState.IsValid) {
int? ParentID = null;
if (parentId != "null") {
ParentID = parentId.AsInt();
}
Category oCategory = new Category();
oCategory.Name = name;
oCategory.Address = address;
oCategory.ParentID = ParentID;
db.Categories.Add(oCategory);
db.SaveChanges();
db.Dispose();
db = null;
}
return RedirectToAction("Categories");
}
在类别查看我有一个纯HTML的形式来获取用户输入来创建一个新的类别,并表示已经我在表单中有
类别我有一个选择框和4块循环遍历父类中的类对孩子的
这里是代码:
<select name="parentID" id="parentCategoryId">
<option value="null" selected>nothing</option>
@foreach (var Category in Model) {
if (Category.ParentID == null) {
<option value="@Category.ID">@Category.Name</option>
foreach (var SubCategory1 in Model) {
if (SubCategory1.ParentID == Category.ID) {
<option value="@SubCategory1.ID">
»
@SubCategory1.Name
</option>
foreach (var SubCategory2 in Model) {
if (SubCategory2.ParentID == SubCategory1.ID) {
<option value="@SubCategory2.ID">
»
@SubCategory2.Name
</option>
foreach (var SubCategory3 in Model) {
if (SubCategory3.ParentID == SubCategory2.ID) {
<option value="@SubCategory3.ID">
»
@SubCategory3.Name
</option>
}
}
}
}
}
}
}
}
</select>
视图我使用从模型中获取数据
:
@model IEnumerable<project.Models.Category>
,但我需要另一个模型对象用于:
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
和验证操作!
问题是我该如何结合这两个视图? 现在我收到此错误:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
对不起我的英语口语!我知道这是可怕的