如何通过 MVC 重新使用回发时的模型数据

发布于 2024-12-28 04:56:45 字数 162 浏览 3 评论 0原文

当我的 MVC 页面最初加载时,我将其传递给它。用户提交联系表单后,我想重新使用相同的数据(我只是“保留”相同的页面),这样我就不必再次访问数据库。我在控制器中声明了一个全局变量来存储模型数据,但在回发结束时它为空,因此看起来我无法在那里重新使用它。

看起来这将是一个典型的场景。我该如何处理?

I'm passing structured data to my MVC page when it loads initially. After the user submits a contact form, I want to re-use the same data (I just "leave" the same page up) so I don't have to hit the database again. I declared a variable global to the controller to store the model data, but it's null at the end of the post back, so it looks like I can't re-use it there.

Seems like this would be a typical scenario. How do I handle it?

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

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

发布评论

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

评论(4

淑女气质 2025-01-04 04:56:45

如果您想重用视图模型或其他不属于回发一部分的检索数据,您可以

a) 将其输出到隐藏字段中,以便将其回发到您的操作 (meh) 或

b) 存储对象(s) 在会话中,以便应用程序中的任何其他控制器/操作都可以使用它。如果您担心内存问题,则可以在重用该会话变量后删除该变量(如果您不需要再次使用它)。

在初始页面加载时,检查会话变量是否存在,如果存在,则一切正常 - 否则填充它。

哦,为什么全局变量不起作用 ->每个请求都会新建一个控制器(假设使用默认控制器工厂),因此控制器中的任何全局变量都将在每个请求时重置。

If you are wanting to reuse viewmodel or other retrieved data that is not going to be part of the postback, you can either

a) Output it in hidden fields so that it is posted back to your action (meh) or

b) Store the object(s) in Session so that it will be available to any other controllers/actions in your application. If you are worried about memory, you could delete that session variable after you reuse it if you are not going to need to use it again.

On your initial page load, check if the session variable exists, if it does, you are good - else populate it.

Oh and why the global variable thing isn't working -> a controller is new'd up for each request (assuming using the default controller factory) and as such any global variables in the controller will be reset on each request.

美羊羊 2025-01-04 04:56:45
public ActionResult Foo()
{
    var model = GetModelFromDB();
    Return View(model);
}

[HttpPost]
public ActionResult Foo(Entity model)
{
    Return View(model);
}

Asp.net-mvc 是无状态,因此每个 HTTP 请求都有不同的上下文,每次您点击控制器时,所有数据都会在构造函数中重置,这就是为什么您会得到 null

如果模型的属性在提交的表单中,您可以在帖子中获取该模型。

public ActionResult Foo()
{
    var model = GetModelFromDB();
    Return View(model);
}

[HttpPost]
public ActionResult Foo(Entity model)
{
    Return View(model);
}

Asp.net-mvc is stateless so each HTTP request has a different context, and each time you hit the controller all it's data reset in the constructor, this why you get null.

You can get the model in the post if it's properties are within the submitted form .

趁微风不噪 2025-01-04 04:56:45

如果您确实不想离开您所在的页面,并且不想像 KMan 建议的那样将所有其他数据发布回来,但仍然想捕获用户的联系信息/数据,您可以使用 ajax 发布联系信息。

If you really don't want to leave the page you are on, and don't want to post all the other data back as KMan suggests, but still want to capture the users contact information/data you could post the contact info using ajax.

孤独陪着我 2025-01-04 04:56:45

如果您将视图模型作为方法的参数,则只需将其返回到回发时的视图即可。例如:

public ActionResult TestAction(MyViewModelType testViewModel)
{
   //Do logic

    return View("view",testViewModel);

}

请注意,您必须在要发布的表单中包含数据。

If you have your view model as an argument to your method, you can just return it to the view on postback. Ex:

public ActionResult TestAction(MyViewModelType testViewModel)
{
   //Do logic

    return View("view",testViewModel);

}

Note that you have to have the data inside the form you are posting.

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