2
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>
Lee
  • 665
  • 3
  • 7
  • 16
  • 1
    The problem could be in your editor template. Can you show the markup for it? – jrummell May 11 '12 at 16:44
  • 2
    You didn't ask a question, try reading your post again. But I can safely say that the illegal parameters in path error is not related to the Concat call. – driis May 11 '12 at 16:47
  • Your IEnumerable generics use an Address class, but your editor template uses a class of type AddressRecords. Is this a typo? Are you saying if instead of the concat call you just set AllAddresses = AddressSet1 it works? – DMulligan May 11 '12 at 18:10
  • Yes if I set set AllAddresses = AddressSet1,then it works ,but I need to combine both – Lee May 11 '12 at 18:11
  • Do you have @using System.Linq in your .cshtml file. It sounds like that is your problem. I think you are using String.Concat by mistake – gprasant May 11 '12 at 18:49

2 Answers2

2

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());
DMulligan
  • 8,993
  • 6
  • 33
  • 34
  • Just to expand on this. It appears that when trying to guess the appropriate template, MVC is looking at the actual runtime type of your enumerable. Concat (and virtually all other LINQ functions) rely on private generic runtime types, which contain '<' and '>' characters that are, of course, illegal in a file path. Unfortunately it appears the only work around is to bypass the template inferencing logic by explicitly iterating the enumerable. – Phil Degenhardt Jan 27 '14 at 22:25
  • @PhilDegenhardt: Wouldn't that mean that the `@foreach` syntax should work while the `@Html.EditorFor` syntax should not work? Since `ToList` works, it seems to be something specific to the iterators themselves, rather than the use of generics at all. – CSJ Jan 05 '15 at 16:33
  • Agree. When I said "explicitly iterating" I meant that I would expect @foreach to work. However, IIRC, this causes other problems with EditorFor(). – Phil Degenhardt Jan 05 '15 at 21:34
  • I wound up solving a similar issue, using `.ToList()`. See my other answer at http://stackoverflow.com/a/27800743/498403. – CSJ Jan 06 '15 at 14:30
0

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
...
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928