MVC5 - 具有复杂类型

时间:2016-05-09 20:13:24

标签: c# asp.net-mvc-5 viewmodel html-helper checkboxfor

我无法找到像这样的问题的解决方案。如果这是一个重复的帖子,我很抱歉。

我正在使用此

建模我的解决方案

MVC3 - Viewmodel with list of complex types

我的ViewModel(ZooAnimalsViewModel)包含可能的动物列表以及ICollection。

我正在尝试填充Zoo和Animal之间的链接表 - 该表是ZooAnimalDetail。我将ZooAnimalsViewModel作为模型传递给主视图 和Animal作为EditorTemplate文件的模型allAnimals.cshtml。

从我所看到的,我应该使用框架来生成列表,方法是在EditorTemplate目录中创建一个与我想要显示的列表/集合同名的文件。

我想显示列表allAnimals并使用它来更新ICollection ZooAnimalDetail中包含的内容 - 我无法弄清楚如何编写一个Html帮助器来绑定它们。

简化的ViewModel ZooAnimalViewModel具有以下属性:

public class ZooAnimalViewModel
    {
        public Zoo zoo { get; set; }
        public ICollection<ZooAnimalDetail> ZooAnimalDetail { get; set; }
        public List<Animal> allAnimals { get; set; }
    }

ZooAnimalDetail的简化模型如下:

  public partial class ZooAnimalDetail
      {
          public int zooID { get; set; }
          public int animalID { get; set; }
          public bool animalDefault { get; set; }
          public virtual Animal Animal { get; set; }
          public virtual Zoo Zoo { get; set; }
      }

Animal的简化模型如下:

public partial class Animal
{
   public Animal()
   {
       ZooAnimalDetails = new HashSet<ZooAnimalDetail>();
   }
   public int animalID { get; set; }
   public string animalName { get; set; }
   public virtual ICollection<ZooAnimalDetail> ZooAnimalDetails { get; set; }
}

在控制器中,我创建一个新的ZooAnimalViewModel对象,并将zoo初始化为当前的zoo和allAnimals,以包含可能可以在该动物园生活的所有动物的列表。

在我看来,我试图在allAnimals列表中显示2个复选框的所有项目。 如果选中复选框1,请将此动物添加到动物园。 如果选中复选框2,则将animalDefault设置为true。

简化视图如下所示

@model ZooAnimalViewModel
@using (Html.BeginForm())
{
    @Html.HiddenFor(x => Model.zoo.zooID)
    <table>
        <tr>
            <th>Animal Name</th>
            <th>Add</th>
            <th>Set as default</th>
        </tr>
        @Html.EditorFor(x => x.allAnimals)
    </table>
    <input type="submit" value="Save" />
}

在〜/ Views / Shared / EditorTemplates / allAnimals.cshtml中我试图设置视图

@model DatabaseName.Models.Animal
@Html.HiddenFor(x => x.animalID)
<tr>
    <td>@Html.CheckBoxFor(x => x.ZooAnimalDetail.??)</td>  @*PROBLEM 1*@
    <td>@Html.CheckBoxFor(x => x.ZooAnimalDetail.Add(animalDefault))</td> @*PROBLEM 2*@
    <td>@Html.DisplayFor(x => x.animalName)</td>
</tr>

问题1 - 我想在列表中显示每个动物的复选框 - 如果选中它我希望将这个动物添加到动物园中 - 因此ZooA和animalID的ZooAnimalDetail将存在。

问题2 - 如果选择将动物添加到动物园并选中第二个复选框,我想将ZooAnimalDetail中的animalDefault设置为true。

我是以正确的方式接近这个吗?

修改

前进

我将布尔字段IsChecked添加到ZooAnimalDetail类

我创建了〜/ Views / Shared / EditorTemplates / ZooAnimalDetail.cshtml如下

  @model DatabaseName.Models.ZooAnimalDetail
  @Html.HiddenFor(x => x.animalID)
  <tr>
      <td>@Html.CheckBoxFor(x => x.IsChecked)</td>
      <td>@Html.CheckBoxFor(x => x.animalDefault)</td>
      <td>@Html.DisplayFor(x => x.Animal.animalName)</td>
  </tr>

而不是使用

视图中的@ Html.EditorFor(x =&gt; x.allAnimals)

我用

@ Html.EditorFor(x =&gt; x.ZooAnimalDetail)

将ZooAnimalViewModel(zavm)传递回Post Controller后,我将其保存到数据库中,以便生成zooID。

我使用foreach循环仅选择那些将IsChecked设置为true的动物来更新ZooAnimalDetail实体。

     if (ModelState.IsValid)
       {
           db.Zoos.Add(zavm.zoo);
           db.SaveChanges();

           //grab the new zooID
           int lastzooID = db.Zoos.Max(item => item.zooID);

           foreach (ZooAnimalDetail z in zavm.ZooAnimalDetail)
           {
               if (z.IsChecked == true)
               {
                    z.zooID = lastzooID;
                    db.ZooAnimalDetails.Add(z);
               }
           }                
            db.SaveChanges();
            return RedirectToAction("Index");
       }

我不再需要ZooAnimalViewModel中的allAnimals列表。

似乎工作正常。

0 个答案:

没有答案