问题描述
我的位置列表
public class Location
{
public string LocationName { get; set; }
public string Address { get; set; }
}
我创建模板编辑器为此类
I created editor template for this class
<div>
<span>@Html.TextBoxFor(a => a.LocationName)</span>
<span style="color: red;">@Html.TextBoxFor(a => a.Address )</span>
</div>
问题是,地点与阿贾克斯加载到我的网页,然后我将结果发送回服务器。
可是如何才能让与特定的指数这个位置?我的意思是,对于第一个位置,它会产生这样的:
The problem is that locations load with ajax to my page and then I am going to send the results back to the server.But how to get this location with specific index? I mean that for the first location it will generate like this:
<input type="text" name="Locations[0].LocationName" />
有关,当我preSS第二位置添加位置按钮,它应该从服务器(从控制器作为HTML字符串的动作)这个位置,但与指数1不是0,因为它是一个位置。
For the second location when I press "Add Location" button it should get this location from the server (from action of controller as html string) but with index 1 not 0 because it is next location.
是否有可能实现吗?还是我做错了什么?
Is it possible to achieve it? Or I am doing something wrong?
推荐答案
像这样
[HttpGet]
public virtual ActionResult LocationEditor(int index)
{
ViewData.TemplateInfo.HtmlFieldPrefix = string.Format("Locations[{0}]", index);
return PartialView(@"EditorTemplateExplicitPathAsOnlyViewNameAintGonnaWork.cshtml");
}
而对于地名容器
<div class="locations-container" data-item-count="@Model.Count">
@for (int j = 0; j < Model.Count; j++)
{
@Html.EditorFor(model => model[j])
}
<a href="#" class="add-location">Add Location</a>
</div>
JavaScript的加入后递增的数据项数的点点的休息和调用 LocationNameEditorLocationNameEditor
新的索引操作是在你们身上。
The rest of tiny bit of javascript to increment data-item-count upon adding and call LocationNameEditorLocationNameEditor
action with new index is upon you.
这篇关于我怎样才能从控制器调用EditorFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!