【问题标题】:Persist ViewModel data from GET to POST将 ViewModel 数据从 GET 持久化到 POST
【发布时间】:2011-05-08 20:42:39
【问题描述】:

我有两种操作方法:

[HttpGet]
public ActionResult Customize()
{
  return View(new CustomizeViewModel { Thing1 = "test", Thing2 = "test" });
}

[HttpPost]
public ActionResult Customize(CustomizeViewModel customizeViewModel)
{
  _someService.DoSomething(customizeViewModel);

  ...
}

我的 ViewModel 看起来像:

public class CustomizeViewModel
{
  public string Thing1 { get; set; }
  public string Thing2 { get; set; }
  public string Thing3 { get; set; }
}

在我的视图中,我有一个文本框,它收集 Thing3 的值并简单地显示 Thing1Thing2 的值。我的问题是,当我 POST 并输入 Customize 方法的 POST 版本时,我只得到 Thing3 的值(我在文本框中输入的那个)。有什么方法可以获取我填充的值GET 版本的 Customize 方法可以继承吗?我试过UpdateModel(),但没用。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-2 viewmodel asp.net-4.0


    【解决方案1】:

    这是创建隐藏输入字段的原因之一。在视图中使用HiddenFor 将值存储在其中,您应该进行设置。在 HTTP POST 版本中将出现的唯一数据是表单输入元素。

    【讨论】:

      【解决方案2】:

      为 Thing1 和 Thing2 添加隐藏字段。

      【讨论】:

        猜你喜欢
        • 2016-08-19
        • 1970-01-01
        • 2016-12-20
        • 1970-01-01
        • 2020-12-09
        • 2015-08-19
        • 2021-09-19
        • 1970-01-01
        • 2012-04-15
        相关资源
        最近更新 更多