将 ViewModel 数据从 GET 保留到 POST
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是创建隐藏输入字段的目的之一。在视图中使用
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.为 Thing1 和 Thing2 添加隐藏字段。
Add hidden fields for Thing1 and Thing2.