MVC在回发后保留值 [英] Mvc retain values after postback

查看:126
本文介绍了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在回发后保留值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆