如何从控制器调用EditorFor

我有位置列表

public class Location { public string LocationName { get; set; } public string Address { get; set; } } 

我为这个类创建了编辑器模板

 
@Html.TextBoxFor(a => a.LocationName) @Html.TextBoxFor(a => a.Address )

问题是,位置加载了ajax到我的页面,然后我将结果发送回服务器。 但是如何使用特定索引获取此位置? 我的意思是,对于第一个位置,它将生成如下:

  

对于第二个位置,当我按“添加位置”按钮时,它应该从服务器获取此位置(从控制器的操作作为html字符串)但索引1不是0,因为它是下一个位置。

有可能实现吗? 或者我做错了什么?

像这样的东西

  [HttpGet] public virtual ActionResult LocationEditor(int index) { ViewData.TemplateInfo.HtmlFieldPrefix = string.Format("Locations[{0}]", index); return PartialView(@"EditorTemplateExplicitPathAsOnlyViewNameAintGonnaWork.cshtml"); } 

并为位置名称容器

 
@for (int j = 0; j < Model.Count; j++) { @Html.EditorFor(model => model[j]) } Add Location

在添加并使用新索引调用LocationNameEditorLocationNameEditor操作时,其余的一小部分javascript用于递增数据项计数。

您可以尝试使用部分视图并创建一个返回AjaxResult的操作方法(我相信这是正确的)。 然后,您可以让控制器填充视图模型并将其传递到要在您指定的位置的页面中呈现的局部视图。