4

我一直在寻找关于如何正确处理具有嵌套属性的模型绑定的良好工作解决方案。我有一个模型,其中包含其他子模型的列表,如下所示:

public class Organization : IEntity
{
    [ScaffoldColumn(false)]
    public int ID
    {
        get; 
        set;
    }

    [LocalizedDisplayName("Goals")]
    public virtual ICollection<OrganizationGoal> Goals
    {
        get;
        set;
    }
}

在控制器中,我尝试像这样更新数据:

[HttpPost]
public ActionResult Edit(string organizationIdentifier, FormCollection values)
{
    var organization = organizationService.GetByIdentifier(organizationIdentifier);

    if (TryUpdateModel(organization))
    {
       organizationService.Save(organization);
       return RedirectToAction("Edit");
    }

    return View("Edit");
}

但是 TryUpdateModel 总是返回 false,并且 UI 中不会显示任何验证消息。UI 是使用标准 MVC 帮助器 EditorFor 构建的。

这样做的最佳做法是什么?对于一个非常正常的情况,没有那么容易找到信息。

谢谢!

4

1 回答 1

0

现在是您使用 GetByIdentifier 查询的 ID 列吗?如果是这样,你为什么要传入一个字符串,但在定义中将它作为一个 int ?

同样通过阅读 TryUpdateModel,听起来您可能想改用 UpdateModel。

http://msdn.microsoft.com/en-us/library/dd460189.aspx

于 2011-01-19T21:14:28.213 回答