0

我有以下动作:

public ActionResult Create()
{
    var entity = new Employee();
    TryUpdateModel(entity, new[] { "Person.Name", "Code", "CompanyID" });
    if (ModelState.IsValid)
    {
        var result = Service.MergeEmployee(entity);
        return RedirectToAction("List", new { success = true });
    }
    return View("Edit", new SupplierEmployeeModel() { Employee = entity });
}

发生的情况是 TryUpdateModel 没有填充属性“Person.Name”。

这是我的表格:

 <fieldset>
    <p>
        <label for="Name"><%=Strings.NAME %></label>
        <%= Html.TextBox("Person.Name", Model.Employee.Person.Name, new { Class = "text" })%>
        <%= Html.ValidationMessage("Name", "*") %>
    </p>
    <p>
        <label for="CompanyID"><%=Strings.SUPPLIER %></label>
        <%= Html.DropDownList("CompanyID") %>
        <%= Html.ValidationMessage("CompanyID", "*")%>
    </p>
    <p>
        <label for="Code"><%=Strings.CODE %></label>
        <%= Html.TextBox("Code", Model.Employee.Code)%>
        <%= Html.ValidationMessage("Code", "*") %>
    </p>
    <p>
        <%= Html.Hidden("ID", Model.Employee.ID)%>
    </p>
    <div id="tabs-DE-actions" class="ui-dialog-buttonpane  ui-helper-clearfix" style="display: block;">
        <button class="ui-state-default ui-corner-all" type="submit"><%=Strings.SAVE%></button>
    </div>
</fieldset>

关于为什么会发生这种情况的任何想法?谢谢

4

3 回答 3

4

确保 Person 对象在 Employee 构造函数中被初始化;如果它一开始是空的,它可能没有正确更新。

public Employee()
{
    Person = new Person();
}
于 2009-07-23T13:34:38.757 回答
0

为了填写 Person.Name,模型绑定器必须创建一个新的 Person。您是否为模型绑定器提供了足够的信息来做到这一点?或者,在绑定之前尝试自己创建 Person。

于 2009-05-21T12:41:40.353 回答
0

尝试这个:

 TryUpdateModel(entity,"Person", new[] { "Name", "Code", "CompanyID" });
于 2009-05-21T10:52:44.877 回答