Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
asp.net mvc3,c#_C#_Asp.net Mvc 3 - Fatal编程技术网

asp.net mvc3,c#

asp.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

我想分别在facebook和twitter上交朋友。 像这样:

TwitterFacebook

编辑模板:FriendModel.cshtml

@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) }
我认为您需要在一个表单中添加多个项目。本文可能会帮助您:

jQuery动态表单是jQuery的插件。它使我们能够 基于HTML模板动态添加字段。正在更新版本1.0 并已上传到谷歌代码。它包括两项期待已久的计划 特点:嵌套字段复制和数据注入。我会更新的 示例当我有时间时,您已经有了一个工作示例 包在zip里,去下载吧!


我会在这里使用一个视图模型,其中1个IEnumerable用于twitter,1个IEnumerable用于facebook,以便在控制器中进行拆分,而不是在@Html.EditorFor(model=>model.Friends.Where(x=>x.SocialNetworkName==“twitter”)中进行拆分,这就是问题所在。我照你说的做了,但问题是一样的。谢谢你做循环?当我使用@Html.EditorFor(model=>model.Friends)时,所有这些都会出现