将 ViewModel 数据从 GET 保留到 POST

发布于 2024-10-02 09:35:15 字数 772 浏览 1 评论 0原文

我有 2 个操作方法:

[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 的值,并仅显示 Thing1的值事情2。我的问题是,当我 POST 并输入 Customize 方法的 POST 版本时,我只获得 Thing3 的值(我在文本框中输入的值。)有什么方法可以获取我在自定义方法的 GET 版本中填充的值以进行继承?我尝试了 UpdateModel() 但没有成功。

I have 2 action methods:

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

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

  ...
}

My ViewModel looks like:

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

In my View, I have a textbox that collects a value for Thing3 and simply shows the values for Thing1 and Thing2. My problem is, when I POST and enter the POST version of the Customize method, I only get a value for Thing3 (the one that I typed into the textbox.) Is there any way to get the values I populated in the GET version of the Customize method to carry over? I tried UpdateModel() but that didn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

镜花水月 2024-10-09 09:35:15

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

This is one of the things for which hidden input fields were created. Store the values in there using HiddenFor in the View and you should be set. The only data that will come along for the ride in the HTTP POST version are form-input elements.

一个人的夜不怕黑 2024-10-09 09:35:15

为 Thing1 和 Thing2 添加隐藏字段。

Add hidden fields for Thing1 and Thing2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文