asp.net mvc3,c#
我想分别在facebook和twitter上交朋友。 像这样: TwitterFacebook 编辑模板:FriendModel.cshtmlasp.net mvc3,c#,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我想分别在facebook和twitter上交朋友。 像这样: TwitterFacebook 编辑模板:FriendModel.cshtml @model App.Web.Models.FriendModel <div> <input type="checkbox" name="saveInputs" value="@Model.FriendID"/> <img alt="" src="@Model.ProfileImageUrl" /> <stron
@model App.Web.Models.FriendModel
<div>
<input type="checkbox" name="saveInputs" value="@Model.FriendID"/>
<img alt="" src="@Model.ProfileImageUrl" />
<strong>@Model.Name</strong> Friends: <strong>@Model.FriendsCount</strong>
</div>
<br />
@model App.Web.Models.FriendTeamModel
@{
ViewBag.Title = "FriendTeam";
}
<h2>
FriendTeam</h2>
@using (@Html.BeginForm())
{ @Html.ValidationSummary(true)
<h3>Twitter</h3>
<br />
@Html.EditorFor(model => model.Friends.Where(x=>x.SocialNetworkName=="Twitter"))
<h3>Facebook</h3>
<br />
@Html.EditorFor(model => model.Friends.Where(x=>x.SocialNetworkName=="Facebook"))
}
<div>
@Html.ActionLink("Back to List", "Index")
错误:
public class FriendTeamModel
{
public long FriendTeam{ get; set; }
public IEnumerable<FriendModel> Friends { get; set; }
}
public class FriendModel
{
public string FriendID { get; set; }
public string SocialNetworkName { get; set; }
public string Name { get; set; }
public int FriendsCount { get; set; }
public string ProfileImageUrl { get; set; }
}
模型只能与表达式、房屋所有权访问字段、一维数组索引或自定义索引器单参数一起使用
谢谢,这个错误基本上意味着你不能这样做:
@Html.EditorFor(model => model.Friends.Where(x=>x.SocialNetworkName=="Twitter"))
如果要显示所有好友,请循环浏览您的IEnumerable Friends
,或者使用过滤方法返回单个FriendModel
,如:
@Html.EditorFor(model => model.Friends.SingleOrDefault(x=>x.SocialNetworkName=="Twitter"))
您应该更改视图模型以更好地适应您的视图
public class FriendTeamModel
{
public long FriendTeam{ get; set; }
public IEnumerable<FriendModel> FacebookFriends { get; set; }
public IEnumerable<FriendModel> TwitterFriends { get; set; }
}
公共类FriendTeamModel
{
公共长FriendTeam{get;set;}
公共IEnumerable FacebookFriends{get;set;}
公共IEnumerable TwitterFriends{get;set;}
}
并在控制器中而不是视图中设置这些参数值。这样,在视图中,您将
@using (@Html.BeginForm())
{
@Html.ValidationSummary(true)
<h3>Twitter</h3>
<br />
@Html.EditorFor(model => model.TwitterFriends)
<h3>Facebook</h3>
<br />
@Html.EditorFor(model => model.FaceookFriends)
}
@使用(@Html.BeginForm())
{
@Html.ValidationSummary(true)
啁啾
@EditorFor(model=>model.TwitterFriends)
脸谱网
@EditorFor(model=>model.FaceookFriends)
}
我认为您需要在一个表单中添加多个项目。本文可能会帮助您:
我会在这里使用一个视图模型,其中1个IEnumerable用于twitter,1个IEnumerable用于facebook,以便在控制器中进行拆分,而不是在@Html.EditorFor(model=>model.Friends.Where(x=>x.SocialNetworkName==“twitter”)中进行拆分,这就是问题所在。我照你说的做了,但问题是一样的。谢谢你做循环?当我使用@Html.EditorFor(model=>model.Friends)时,所有这些都会出现