0

I want separate friends facebook and twitter. Like this :

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

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

Error :

Models can only be used with expressions access field of homeownership, the index of one-dimensional array or custom indexer single parameter.

Thanks,

1
  • 1
    I would use a view model here with 1 IEnumerable<FriendModel> for twitter and one for facebook so that the splitting is done in the controller and not the view Commented May 23, 2012 at 13:31

3 Answers 3

1

The error basically means you can't do this :

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

Either loop through your IEnumerable<FriendModel> Friends if you want to display them all or use a filtering method that returns a single FriendModel, like :

@Html.EditorFor(model => model.Friends.SingleOrDefault(x=>x.SocialNetworkName=="Twitter"))
Sign up to request clarification or add additional context in comments.

2 Comments

exactly what is in @Html.EditorFor(model => model.Friends.Where(x=>x.SocialNetworkName=="Twitter")) that are the problem. I did what you said, but same problem. Thanks
how to make the loop? when i use @Html.EditorFor(model => model.Friends) all appear
0

You should change your view model to better suit your view

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

And set those parameter values in controller, not in view. This way, in view, you will have

@using (@Html.BeginForm())
{          
  @Html.ValidationSummary(true)     
  <h3>Twitter</h3>
  <br />    
   @Html.EditorFor(model => model.TwitterFriends)                  

  <h3>Facebook</h3>
  <br />    
   @Html.EditorFor(model => model.FaceookFriends) 
 }

Comments

0

I think you need add multiple items in one form. this article maybe help you:

jQuery Dynamic Form is a plugin for jQuery. It gives the ability to dynamically add fields based on HTML template. Update version 1.0 is out and has been uploaded on Google Code. It includes two long awaited features : nested fields duplication, and data injection. I’ll update examples when I have time, however you already have a working example package in the zip, go download it ! http://code.google.com/p/jquery-dynamic-form/downloads/list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.