【发布时间】: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<Requirement>的属性,因此您视图中的模型只需为Project。但是,如果您有Requirement的集合,public int RequirementID { get; set; }就没有意义 -
上面的 cmets 说 .. 您的代码正在尝试从 IEnumerable 访问“shortDesc”(Item2 不是“要求”,它是一个集合)
标签: c# asp.net-mvc twitter-bootstrap