MVC两个模型相同的视图模型IEnumerable问题



我正在尝试学习MVC,并想出了一个小项目来解决它。我有两种型号:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace reqcoll.Models
{
  public class Project
  {
    [Key]
    public int ProjectID { get; set; }
   [Required]
   [DataType(DataType.Text)]
   [MaxLength(150, ErrorMessage = "The project name cannot be longer than 150 characters!")]
   [Display(Name = "Project name:")]
   public string projectName { get; set; }
   [Required]
   [Display(Name = "Project type:")]
   public ProjectType projectType { get; set; }
   public int RequirementID { get; set; }
   public virtual List<Requirement> Requirements { get; set; }
  }
}

using System.ComponentModel.DataAnnotations;
namespace reqcoll.Models
{
  public class Requirement
  {
    [Key]
    public int requirementID { get; set; }
    [DataType(DataType.Text)]
    [MaxLength(150, ErrorMessage = "This is a short description, it cannot be longer than 150 characters.")]
    [Required]
    public string shortDesc { get; set; }
    [DataType(DataType.Text)]
    [MaxLength(3999, ErrorMessage = "This is a short description, it cannot be longer than 150 characters.")]
    [Required]
    public string longDesc { get; set; }
    [Required]
    public int priorityCode { get; set; }
    [Required]
    public int OrderID { get; set; }
    public int projectID { get; set; }
  }
}

我有一个观点:

@using reqcoll.Models;
@model Tuple<Project, IEnumerable<Requirement> >

@{
  ViewBag.Title = "ReqColl - project";
}
<div class="top-spacing col-md-12 col-lg-12 col-sm-12">
  <div class=" well">
    @Html.ValidationSummary(true)
    <div class="row">
      @Html.LabelFor(tuple => tuple.Item1.projectName, htmlAttributes: new {     @class = "control-label col-md-2 col-lg-2 col-sm-12" })
      <div class="col-md-10 col-lg-10 col-sm-12">
        @Html.TextBoxFor(tuple => tuple.Item1.projectName, htmlAttributes: new {     @class = "ProjectNameInput" })
        @Html.ValidationMessageFor(tuple => tuple.Item1.projectName)
      </div>
    </div>
    @Html.ValidationSummary(true)
    <div class="row row-spacing">
      @Html.LabelFor(tuple => tuple.Item1.projectType, htmlAttributes: new {     @class = "control-label col-md-2 col-lg-2 col-sm-12" })
      <div class="col-md-10 col-lg-10 col-sm-12">
        @Html.DropDownListFor(tuple => tuple.Item1.projectType, new SelectList(
                  new List<Object>{
                       new { value = 0 , text = "...Select..."  },
                       new { value = 1 , text = "Windows application"  },
                       new { value = 2 , text = "Web application" },
                       new { value = 3 , text = "Device application"}
                    },
                  "value",
                  "text",
                   0), htmlAttributes: new { @class = "DropDownList" })
        @Html.ValidationMessageFor(tuple => tuple.Item1.projectType)
      </div>
    </div>
  </div>
</div>
<div class="top-spacing col-md-6 col-lg-6 col-sm-12">
  <div class=" well">
    <table class="table">
      <tr>
        <th>
          @Html.DisplayNameFor(tuple => tuple.Item2.shortDesc)
        </th>
        <th></th>
      </tr>
      @foreach (Requirement item in (IEnumerable<Requirement>)Model.Item2)
      {
        <tr>
          <td>
            @Html.DisplayFor(modelItem => item.shortDesc)
          </td>
          <td>
            @Html.ActionLink("E", "Edit", new { id = item.requirementID }) |
            @Html.ActionLink("D", "Delete", new { id = item.requirementID })
           </td>
        </tr>
      }
    </table>

  </div>
</div>
<div class="top-spacing col-md-6 col-lg-6 col-sm-12">
  <div class=" well">

  </div>
</div>

当我运行网站时,我得到一个错误:

CS1061:"IEnumerable"不包含"shortDesc"的定义,并且找不到接受"IEnumarable"类型的第一个参数的扩展方法"shortDesc"(是否缺少using指令或程序集引用?)

错误出现在"@Html.DisplayNameFor(tuple=>tuple.Item2.shortDesc)"行的视图中

由于某些原因,它在Item2中看不到属性"shortDesc"。我不明白为什么不。

感谢您的帮助。

您面临的问题是元组。Item2是一个集合,因此不存在元组。项目2.shortDesc如你所说。

你需要做一些类似的事情:

@Html.DisplayFor(tuple => tuple.Item2)

然后你将需要一个显示模板,它将知道用传入的集合渲染什么

这个问题以前已经问过了,这将引导你走上正确的道路:

ASP.net MVC-集合的显示模板

Item2属性指向模型中所示的IEnumerable<Requirement>

@model Tuple<Project, IEnumerable<Requirement>>

我可以看到你已经在这个代码中列举了Item2

@foreach (Requirement item in (IEnumerable<Requirement>)Model.Item2)

您已经呼叫了item.shortDesc。这是重复呼叫吗?

最新更新