2017-01-03 5 views
0

Erstellen und Index Ansichten in einer Ansicht Kombination Ich habe einen Controller für meine Website Armaturenbrett und eine Aktion für die Rückkehr Kategorien als Liste anzeigen und eine andere Aktion neue Kategoriein Asp.Net MVC 5

Meine Aktionen zu erstellen:

  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"); 
     } 

in den Kategorien anzeigen i ein reines hTML-Formular haben Benutzereingaben zu erhalten, eine neue Kategorie und einen Block zu erstellen, den die Kategorien dar, die bereits i

in Form habe ich eine Auswahlbox und 4 foreach Schleifen, die durch Kategorien von Parent iteriert Childs hier

ist der Code:

    <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> 

in der ich die Daten von Modell zu bekommen betrachten:

@model IEnumerable<project.Models.Category> 

aber ich brauche Objekt ein anderes Modell zu verwenden:

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

und Validierungsvorgang!

Die Frage ist, wie kann ich diese beiden Ansichten kombinieren? und jetzt bin ich immer diese Fehlermeldung:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. 

traurig über mein Englisch zu sprechen! Ich weiß, es ist schrecklich

Antwort

2

Sie erstellen müssen Modell:

public class CategoryModel { 

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

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

In Ihrem Controller:

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"); 
} 

und in der Ansicht:

@model project.Models.CategoryModel 

jetzt können Sie erstellen Felder wie in der gleichen Ansicht:

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