2012-05-23 29 views
0

我想單獨的朋友facebook和twitter。 像這樣:asp.net mvc3,c#

Twitter的Facebook的

EditorTemplates: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 /> 

瀏覽:FriendTeam.cshtml

@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") 

FriendTeamModel:

public class FriendTeamModel 
{ 
    public long FriendTeam{ get; set; } 
    public IEnumerable<FriendModel> Friends { get; set; } 
} 

FriendModel:

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; } 
} 

錯誤:

模型只能用於房屋所有權的表達式訪問字段,一維數組索引或自定義索引器單一參數。

感謝,

+1

我會在這裏使用一個視圖模型1的IEnumerable 對Twitter和一個爲Facebook這樣的拆分在控制器完成的,而不是視圖 – Manatherin

回答

0

您應該將視圖模型更改爲更好的s你的看法

public class FriendTeamModel 
{ 
    public long FriendTeam{ get; set; } 
    public IEnumerable<FriendModel> FacebookFriends { get; set; } 
    public IEnumerable<FriendModel> 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) 
} 
+0

非常感謝, – saisneImane

1

錯誤基本上意味着你不能做到這一點:

@Html.EditorFor(model => model.Friends.Where(x=>x.SocialNetworkName=="Twitter")) 

要麼循環通過您IEnumerable<FriendModel> Friends如果你想顯示他們所有或使用返回的過濾方法單個FriendModel,如:

@Html.EditorFor(model => model.Friends.SingleOrDefault(x=>x.SocialNetworkName=="Twitter")) 
+0

什麼是@ Html.EditorFor(型號=> model.Friends.Where(x => x.SocialNetworkName ==「Twitter」))是問題所在。我做了你所說的,但同樣的問題。謝謝 – saisneImane

+0

如何使循環?當我使用@ Html.EditorFor(model => model.Friends)全部出現 – saisneImane

0

我認爲你需要在一種形式添加多個項目。這篇文章可能幫助您:

jQuery的動態表單是的jQuery插件。它使 能夠根據HTML模板動態添加字段。更新版本1.0是 ,已上傳到Google代碼。它包括兩個期待已久的 功能:嵌套字段重複和數據注入。我會更新 的例子,當我有時間的時候,然而你已經有一個工作實例 在zip包中,去下載吧! http://code.google.com/p/jquery-dynamic-form/downloads/list

+0

感謝Nildarar – saisneImane