append two IEnumerable items

2019-05-23 09:46发布

IEnumerable<Addresses> AddressSet1=myServices.GetAddresses(LocationId1);
IEnumerable<Addresses> AddressSet2=myServices.GetAddresses(LocationId2);

I want to combine the above two AddressSets

I tried IEnumerable<Addresses> AllAddresses=AddressSet1.Concat(AddressSet2)

But after this when I try to access items from IEnumerable AllAddresses by on my razor view

 @if(!myHelper.IsNullorEmpty(model.AllAddresses )
   {
     @Html.EditorFor(model => model.AllAddresses  )
   }

and I am getting errors -- Illegal characters in path .Any suggestions to identify cause of this error ?

If I am trying to run my page with out the Concat I am able see the records in AddressSet1 /AddressSet2 displayed on the page .But when I try to combine the two to form I Enumerable AllAddresses ,it is throwing errors please help

pasted below is my Editor Template

@model MyServiceRole.Models.Addresses
@{
    ViewBag.Title = "All addresses Items";

    }
<table>
<tr>
 <td>
 <span>Index</span>

   </td>
   <td>

</tr>
 <tr>
 <td>Address XID</td>
 <td>
@Html.EditorFor(model => model.AddressID)
</td>
</tr>
 <tr>
 <td>Title</td>
 <td>
@Html.EditorFor(model => model.Title)
</td>
</tr>
<tr>
 <td>Description</td>
 <td>
 @Html.EditorFor(model => model.Description)
</td>
</tr>
<tr>
 <td>Image URL</td>
 <td>
 @Html.EditorFor(model => model.Photo.URL)
</td>
</tr>
</table>

2条回答
趁早两清
2楼-- · 2019-05-23 10:06

If you want to use editor templates why are you writing foreach loops? You don't need this loop at all. Simply write the following and get rid of the foreach:

@Html.EditorFor(x => x.AllAddresses)

and then you will obviously have a corresponding editor template that ASP.NET MVC will automatically render for each element of the AllAddresses collection so that you don't need to write any foreach loops in your view (~/Views/Shared/EditorTemplates/Address.cshtml):

@model Address
...
查看更多
淡お忘
3楼-- · 2019-05-23 10:24

I tested your issue and ran into the same problem.

List<string> a = new List<string>{ "a" };
List<string> b = new List<string>{ "b" };

IEnumerable<string> concat = a.Concat<string>(b);
foreach(string s in concat) { } // this works

return View(concat);

In view:

@model IEnumerable<string>

@foreach(string s in Model)  //This blows up
{
}
@Html.EditorFor(m => Model) //Also blows up

It looks like you honestly can't use templates with or enumerate over the

System.Linq.Enumerable.ConcatIterator<T>

class that Concat creates within a View. This seems like a bug.

Anyway adding .ToList() fixes your issue.

return View(concat.ToList());
查看更多
登录 后发表回答