【问题标题】:MVC two models same view model IEnumerable issueMVC 两个模型相同的视图模型 IEnumerable 问题
【发布时间】:2015-11-30 21:33:37
【问题描述】:

我正在尝试学习 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 指令或程序集引用?)

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

由于某种原因,它在 Item2 中看不到属性“shortDesc”。 我不知道为什么不。

感谢任何帮助。

【问题讨论】:

  • 您不能使用Tuple 生成表单控件(您生成的name 属性与模型完全没有关系)。由于Project 包含IEnumerable&lt;Requirement&gt; 的属性,因此您视图中的模型只需为Project。但是,如果您有 Requirement 的集合,public int RequirementID { get; set; } 就没有意义
  • 上面的 cmets 说 .. 您的代码正在尝试从 IEnumerable 访问“shortDesc”(Item2 不是“要求”,它是一个集合)

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


【解决方案1】:

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

您需要执行以下操作:

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

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

以前有人问过这个问题,这将引导您走向正确的道路:

ASP.net MVC - Display Template for a collection

【讨论】:

    【解决方案2】:

    Item2 属性指向模型中所示的IEnumerable&lt;Requirement&gt;

    @model Tuple<Project, IEnumerable<Requirement>>
    

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

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

    并且您已经调用了 item.shortDesc。这是重复调用吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多