代码之家  ›  专栏  ›  技术社区  ›  My Other Me

如何仅更新模型的一个属性

  •  1
  • My Other Me  · 技术社区  · 14 年前

    我的viewmodel有一个名为Recipient的属性。有一个叫做MobileNumber的属性

    我正在MVC 2中尝试:

    UpdateModel(viewmodel, new[] { "Recipient_MobileNumber" }); // I  expected this to work
    

    2 回复  |  直到 14 年前
        1
  •  1
  •   John Farrell    14 年前

    尝试:

    UpdateModel(viewmodel.Recipient, new[] { "MobileNumber" });
    

    您的问题是使用字符串[]includes作为视图数据表达式,该表达式假设会在对象图上跳跃,以对您需要的内容进行建模绑定。

    UpdateModel不是这样工作的。这些字符串只是用作属性的过滤器。

        2
  •  0
  •   BritishDeveloper    14 年前

    我想是这样的:

    public ActionResult Edit(int id /* id of recipient? */, FormCollection formValues)
    {
        var viewmodel = GetViewModel(id);
        viewmodel.Recipient.ModileNumber = formValues["Recipient.MobileNumber"];
    }