0

通过更新 ViewModel 时

[HttpPost]
public ActionResult Edit(int id, AddressChooserEnum addressChooser, string btnSubmit, FormCollection fc)
{
    var vm = new AddressViewModel(id, addressChooser, bool.FalseString);
    vm.Address = this.repository.GetAddressById(id, addressChooser);
    try
    {
        UpdateModel(vm);
        this.repository.Save();
        return RedirectToAction("Edit", "Customer", new { id = id });
    }
    catch (Exception e)
    {
        return View(vm);
    }
}

一切正常。但我无法与创作合作

[HttpPost]
public ActionResult Create(int id, AddressChooserEnum addressChooser, string btnSubmit, FormCollection fc)
{
    var vm = new AddressViewModel(id, addressChooser, bool.TrueString);
    this.repository.AddAddress(); //What here? It's the simple Add method which needs an Address object.
    this.repository.Save();
    return View(vm);
}

我正在尝试使用“空”地址创建 AddressViewModel。但是如何将它与 FormCollection 一起存储呢?我可以以某种方式将 FormCollection 绑定到一个新的 Address 对象吗?然后添加应该工作。提前致谢。

4

1 回答 1

2

我不会使用表单集合。我只是尝试直接绑定到 View 模型。如果没有看到您的视图,很难告诉您您的操作应该是什么样子。查看这篇关于模型绑定的文章。它应该有帮助!模型绑定

于 2011-12-13T19:30:36.730 回答