使用ASP.NET MVC和Razor添加相关实体?

时间:2015-03-29 02:03:33

标签: c# asp.net-mvc razor asp.net-mvc-5 one-to-many

我有一个Person类,其导航属性为ICollection<Address> AddressesAddress实体具有属性City

我想为用户提供创建/更新Person及其地址的选项,包括添加和删除地址。

因此,为了显示地址,我只需调用@Html.EditorFor(m => m.Addresses),剃刀引擎使用驻留在 EditorTemplates 文件夹中的自定义模板来处理集合,生成foreach字段匹配的签名比如id="Addresses_0__City" name="Addresses[0].City" 到目前为止一切都很好。

问题是添加新的Address es 我创建了一个按钮,当单击时,jQuery函数调用一个动作(通过自定义 EditorTemplate 呈现),但是它的字段没有上面那个签名,但只是id="City" name="City",因此,作为Person实体的一部分,在帖子操作中无法识别。

如何使用正确的签名生成idname个字段?

我已阅读this文章及其他许多文章,但未找到解决idname问题的文章。

1 个答案:

答案 0 :(得分:1)

根据评论建议,我最终使用了以下内容。

无论如何,我必须将新项目包装为集合,并且隐藏字段仅在收集项目之后附加,而不是被注入(因为在删除它时会保留在那里)。

所以我最后添加了以下扩展,以便在Razor cshtml 文件中使用,以及在向集合添加新项目时调用的操作:

以下是扩展程序(还有一些重载,请参阅完整代码here):

private static string EditorForManyInternal<TModel, TValue>(HtmlHelper<TModel> html, Expression<Func<TModel, IEnumerable<TValue>>> expression, IEnumerable<TValue> collection, string templateName)
{
  var sb = new StringBuilder();

  var prefix = html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix;
  var htmlFieldName = (prefix.Length > 0 ? (prefix + ".") : String.Empty) + ExpressionHelper.GetExpressionText(expression);

  var items = collection ?? expression.Compile()(html.ViewData.Model);
  foreach (var item in items)
  {
    var guid = Guid.NewGuid().ToString();

    var dummy = new { Item = item };
    var memberExp = Expression.MakeMemberAccess(Expression.Constant(dummy), dummy.GetType().GetProperty("Item"));
    var singleItemExp = Expression.Lambda<Func<TModel, TValue>>(memberExp, expression.Parameters);

    var editor = html.EditorFor(singleItemExp, templateName, string.Format("{0}[{1}]", htmlFieldName, guid));
    var hidden = String.Format(@"<input type='hidden' name='{0}.Index' value='{1}' />", htmlFieldName, guid);

    var eNode = HtmlNode.CreateNode(editor.ToHtmlString().Trim());
    if (eNode is HtmlTextNode)
      throw new InvalidOperationException("Unsuported element.");

    if (eNode.GetAttributeValue("id", "") == "")
      eNode.SetAttributeValue("id", guid);

    var hNode = HtmlNode.CreateNode(hidden);
    eNode.AppendChild(hNode);
    sb.Append(eNode.OuterHtml);
  }

  return sb.ToString();
}

public static MvcHtmlString EditorForMany<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, IEnumerable<TValue>>> expression, string templateName)
{
  var value = EditorForManyInternal(html, expression, null, templateName);
  return new MvcHtmlString(value);
}

视图中的用法:

<div>
  <h4>@Resources.Person.Children</h4>
  <ul id="patientChildren" class="list-group ajax-collection">
    @Html.EditorForMany(m => m.Children)
  </ul>

  @Ajax.ActionLink("Create Child", "CreateChild", new { patientId = Model.Id, lastName = Model.LastName }, new AjaxOptions { UpdateTargetId = "patientChildren", InsertionMode = InsertionMode.InsertAfter, OnSuccess = CommonData.AjaxOnSuccessJsFuncName }, new { @class = "button btn-default" })
</div>

这是被调用的ajax函数(将生成的项目归类为ajax-collection-item并将删除按钮归类为btn remove)非常重要:

//#region Ajax add and remove
var ajaxCollectionItemSelector = '.ajax-collection-item';
function attachAjaxRemoveHandlers(id) {
  var context = $(id ? '#' + id : ajaxCollectionItemSelector);

  var removeButton = context.find('.btn.remove');

  removeButton.click(function () {
    var button = $(this);
    var collectionItem = button.closest(ajaxCollectionItemSelector);
    collectionItem.remove();
  });
};

function ajaxOnSuccess(ajaxContext) {
  var collectionItem = $(ajaxContext);
  var id = collectionItem.prop('id');
  attachAjaxRemoveHandlers(id);
  //TODO: following line doesn't work
  collectionItem.find(':text:first-of-type').focus();
};

function runCommonScripts() {
  attachAjaxRemoveHandlers();
};
//#endregion Ajax add and remove

新项目操作(CreateChild)如下所示(EditorForSingle扩展程序位于the same place

public ContentResult CreateChild(int patientId, string lastName)
{
  return this.EditorForSingle((Patient p) => p.Children, 
    new PatientChild
    { 
      PatientId = patientId, 
      LastName = lastName 
    });
}
相关问题