MVC在回发后保留值

编程入门 行业动态 更新时间:2024-10-27 00:22:42
本文介绍了MVC在回发后保留值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个简单的TestController类和用户模型:

I have a simple TestController class and User model:

public class TestController : Controller { public ActionResult TestAction() { return View(); } [HttpPost] public ActionResult TestAction(User user) { return View(); } } public class User { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } }

这是我的表格:

据我所知,MVC是无状态的,并且没有viewstate概念.但是,将数据发布到我的控制器后,当我返回视图时,我所有的数据都在那里.我希望有空字段,但它们都充满了已发布的数据.我不明白MVC在回发后如何知道这些值?

As far as I know MVC is stateless and it does not have a viewstate concept. But after posting the data to my controller and when I return the view, all my data is there. I expect empty fields but they are all filled with the posted data. I could not understand how MVC knows the values after postback?

推荐答案

@ stephen.vakil

@stephen.vakil

从HttpPost返回View时,假定您正在处理错误情况.它将发布的数据保留在ModelState中,然后在页面上重新填充数据,以便用户可以对其进行纠正.

When you return a View from an HttpPost the assumption is that you are handling an error condition. It will keep the posted data in the ModelState and re-fill the data on the page so that the user can correct it.

更多推荐

MVC在回发后保留值

本文发布于:2023-10-12 21:13:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485913.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MVC   回发后

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!