C# 无法设置EditorFor值(ASP.NET MVC)
我正在编写ASP.NET应用程序 我有这样的模型C# 无法设置EditorFor值(ASP.NET MVC),c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我正在编写ASP.NET应用程序 我有这样的模型 public class InvitationMails { [Key] public int Individ_Id { get; set; } public string From { get; set; } [Display (Name = "Почта")] public string To { get; set; } public string Subject { ge
public class InvitationMails
{
[Key]
public int Individ_Id { get; set; }
public string From { get; set; }
[Display (Name = "Почта")]
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public string Link { get; set; }
public string Name { get; set; }
}
@model IEnumerable<SmartSolutions.Models.InvitationMails>
<div class="inner-div3">
<div class="right-welcome-div2">
<table class="table">
<tr style=" width: 100%">
<th style="font-size: 20px;padding-left: 60px;">
@Html.DisplayNameFor(model => model.To)
</th>
</tr>
<tr>
<td style="height: 40px; width: 40px;">
<a href='@Url.Action("Create", "InvitationMails")'>
<img style="object-fit: cover;" src='@Url.Content("~/Images/plus.png")' />
</a>
<input type="" value="Send"/>
</td>
</tr>
@foreach (var item in Model)
{
<tr>
<td style="font-size: 14px; padding-left: 20px; padding-top: 10px;">
@Html.DisplayFor(modelItem => item.To)
</td>
<td style="text-align: end;padding-top: 10px;">
<a href='@Url.Action("Edit", "InvitationMails", new {id = item.Individ_Id})'>
<img src='@Url.Content("~/Images/Edit.png")' />
</a>
<a href='@Url.Action("Delete", "InvitationMails", new {id = item.Individ_Id})'>
<img src='@Url.Content("~/Images/Delete.png")' />
</a>
</td>
</tr>
}
</table>
</div>
像这样看
public class InvitationMails
{
[Key]
public int Individ_Id { get; set; }
public string From { get; set; }
[Display (Name = "Почта")]
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public string Link { get; set; }
public string Name { get; set; }
}
@model IEnumerable<SmartSolutions.Models.InvitationMails>
<div class="inner-div3">
<div class="right-welcome-div2">
<table class="table">
<tr style=" width: 100%">
<th style="font-size: 20px;padding-left: 60px;">
@Html.DisplayNameFor(model => model.To)
</th>
</tr>
<tr>
<td style="height: 40px; width: 40px;">
<a href='@Url.Action("Create", "InvitationMails")'>
<img style="object-fit: cover;" src='@Url.Content("~/Images/plus.png")' />
</a>
<input type="" value="Send"/>
</td>
</tr>
@foreach (var item in Model)
{
<tr>
<td style="font-size: 14px; padding-left: 20px; padding-top: 10px;">
@Html.DisplayFor(modelItem => item.To)
</td>
<td style="text-align: end;padding-top: 10px;">
<a href='@Url.Action("Edit", "InvitationMails", new {id = item.Individ_Id})'>
<img src='@Url.Content("~/Images/Edit.png")' />
</a>
<a href='@Url.Action("Delete", "InvitationMails", new {id = item.Individ_Id})'>
<img src='@Url.Content("~/Images/Delete.png")' />
</a>
</td>
</tr>
}
</table>
</div>
@model IEnumerable
@DisplayNameFor(model=>model.To)
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.To)
}
我还想设置一个@Html.EditorFor
字段,并像这样编写@Html.EditorFor(model=>model.to)
,但我有这个错误。我不明白为什么,因为我的模型中有到
如何解决这个问题?您的模型是IEnumerable,但“To”是其元素的属性。 您应该获得一个元素,然后使用EditorFor。 例如:
EditorFor(model => model.First().To)
您正在尝试使用foreach块之外的一个项的属性。您的模型不是一个项目,而是多个项目,因此当您访问“访问”属性时,必须为模型中的单个项目访问该属性。您应该考虑模型是否为空序列。例如,
@if(Model.Count()>0){EditorFor(Model=>Model.First().To)}
。为什么要EditorFor()
-您甚至没有表单,为什么要创建输入?