Asp.net mvc 3 附加两个IEnumerable项
我得到了错误——路径中的非法字符。有什么建议来确定这个错误的原因吗 如果我试图在没有Concat的情况下运行我的页面,我可以看到页面上显示的AddressSet1/AddressSet2中的记录。但是,当我试图将两者结合起来形成我可枚举的所有地址时,它会抛出错误,请帮助 下面粘贴的是我的编辑器模板Asp.net mvc 3 附加两个IEnumerable项,asp.net-mvc-3,c#-4.0,razor,Asp.net Mvc 3,C# 4.0,Razor,我得到了错误——路径中的非法字符。有什么建议来确定这个错误的原因吗 如果我试图在没有Concat的情况下运行我的页面,我可以看到页面上显示的AddressSet1/AddressSet2中的记录。但是,当我试图将两者结合起来形成我可枚举的所有地址时,它会抛出错误,请帮助 下面粘贴的是我的编辑器模板 @if(!myHelper.IsNullorEmpty(model.AllAddresses ) { @Html.EditorFor(model => model.AllAdd
@if(!myHelper.IsNullorEmpty(model.AllAddresses )
{
@Html.EditorFor(model => model.AllAddresses )
}
@model MyServiceRole.Models.Addresses
@{
ViewBag.Title=“所有地址项”;
}
指数
地址XID
@EditorFor(model=>model.AddressID)
标题
@EditorFor(model=>model.Title)
描述
@EditorFor(model=>model.Description)
图像URL
@EditorFor(model=>model.Photo.URL)
如果要使用编辑器模板,为什么要编写foreach
循环?你根本不需要这个循环。只需编写以下内容,就可以摆脱foreach的
@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>
然后,您显然将拥有一个相应的编辑器模板,ASP.NET MVC将为AllAddresses
集合的每个元素自动呈现该模板,这样您就不需要在视图中编写任何foreach循环(~/Views/Shared/EditorTemplates/Address.cshtml
):
我测试了你的问题,遇到了同样的问题
@model Address
...
问题可能在编辑器模板中。你能显示它的标记吗?你没有问任何问题,请再次阅读你的文章。但是我可以放心地说,path error中的非法参数与Concat调用无关。IEnumerable泛型使用Address类,但编辑器模板使用AddressRecords类型的类。这是打字错误吗?你是说如果你不调用concat,而只是设置AllAddresses=AddressSet1,它就可以工作了吗?是的,如果我设置了set AllAddresses=AddressSet1,它就可以工作了,但是我需要将两者结合起来,你的.cshtml文件中有@using System.Linq。听起来这是你的问题。我认为您错误地使用了String.Concat只是为了扩展它。在尝试猜测适当的模板时,MVC似乎正在查看可枚举的实际运行时类型。Concat(以及几乎所有其他LINQ函数)依赖于私有泛型运行时类型,其中包含“”字符,当然,这些字符在文件路径中是非法的。不幸的是,唯一的解决办法似乎是通过显式迭代枚举来绕过模板推断逻辑。@PhilDegenhardt:这难道不意味着@foreach
语法应该起作用,而@Html.EditorFor
语法不应该起作用吗?由于ToList
有效,它似乎是迭代器本身特有的,而不是泛型的使用。当我说“显式迭代”时,我的意思是希望@foreach能够工作。但是,IIRC会导致EditorFor()出现其他问题。我使用.ToList()
解决了类似的问题。请参阅我的另一个答案。
@Html.EditorFor(x => x.AllAddresses)
@model Address
...
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);
@model IEnumerable<string>
@foreach(string s in Model) //This blows up
{
}
@Html.EditorFor(m => Model) //Also blows up
System.Linq.Enumerable.ConcatIterator<T>
return View(concat.ToList());