2017-01-03 54 views
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"> 
              &nbsp;&nbsp;&nbsp; 
              » 
              &nbsp; 
              @SubCategory1.Name 
             </option> 
             foreach (var SubCategory2 in Model) { 
              if (SubCategory2.ParentID == SubCategory1.ID) { 
               <option value="@SubCategory2.ID"> 
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                » 
                &nbsp; 
                @SubCategory2.Name 
               </option> 
               foreach (var SubCategory3 in Model) { 
                if (SubCategory3.ParentID == SubCategory2.ID) { 
                 <option value="@SubCategory3.ID"> 
                  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                  » 
                  &nbsp; 
                  @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. 

对不起我的英语口语!我知道这是可怕的

回答

2

您需要创建模型:

public class CategoryModel { 

     public string Name {get;set;} 
     public string Address {get;set;} 
     pubic string ParentId {get;set;} 

     public IEnumerable<Category> Categories {get;set;} 
} 

在你的控制器:

public ActionResult Categories() { 
     var model = new CategoryModel(); 

     model.Categories = db.Categories.OrderBy(Categories => Categories.Name).ToList(); 

     return View(model); 
} 

[HttpPost] 
public RedirectToRouteResult NewCategory(CategoryModel model) { 
    if (ModelState.IsValid) { 
      int? ParentID = null; 

      if (model.ParentId != "null") { 
       ParentID = model.ParentId.AsInt(); 
      } 

      Category oCategory = new Category(); 
      oCategory.Name = Model.Name; 
      oCategory.Address = Model.Address; 
      oCategory.ParentID = ParentID; 
      db.Categories.Add(oCategory); 
      db.SaveChanges(); 
      db.Dispose(); 
      db = null; 
    } 

    return RedirectToAction("Categories"); 
} 

,并在视图:

@model project.Models.CategoryModel 

现在你可以创建像场在同一视图中:

@Html.EditorFor(Model=>model.Title) 
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })