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

时间:2015-11-30 21:33:37

标签: c# asp.net-mvc twitter-bootstrap

我正在尝试学习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'的定义,并且没有可以找到接受类型'IEnumerable'的第一个参数的扩展方法'shortDesc'(你是否缺少using指令或汇编引用?)< / p>

错误出现在“@ Html.DisplayNameFor(tuple =&gt; tuple.Item2.shortDesc)”

行的视图中

由于某种原因,它没有在Item2中看到属性“shortDesc”。 我无法弄清楚为什么不。

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您遇到的问题是tuple.Item2是一个集合,因此您声明时没有tuple.Item2.shortDesc。

您需要执行以下操作:

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

然后,您将需要一个显示模板,该模板将知道传入的集合要呈现的内容。

之前已经提出过这个问题,这将引导您走上正确的道路:

ASP.net MVC - Display Template for a collection

答案 1 :(得分:0)

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

@model Tuple<Project, IEnumerable<Requirement>>

我可以看到您已在此代码中枚举Item2

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

您已经调用了item.shortDesc。这是重复的电话吗?

相关问题