简体   繁体   English

ASP.NET MVC 2.0-发布后ViewModel中的数据丢失

[英]ASP.NET MVC 2.0 - data in ViewModel lost after post

I have a Controller called ProductController. 我有一个称为ProductController的控制器。 The Controller code is below: 控制器代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Services.Abstract;
using Web.Models;
using Ninject;
using Services.Entities;

namespace Web.Controllers
{
    public class ProductController : Controller
    {

        IProductRepository productRepository;

        public ProductController(IProductRepository products)
        {
            productRepository = products;
        }


        [HttpGet]
        public ActionResult Create() {

            Product p = new Product {
                Id = 5
            };

            string theTitle = "The Title";

            var viewModel = new ProductViewModel {
                Product = p,
                TheTitle = theTitle
            };

            return View(viewModel);

        }

        [HttpPost]
        public ActionResult Create(ProductViewModel pvm) {

            if (ModelState.IsValid) {
                int result = productRepository.SaveProduct(pvm.Product);
                return Content(result.ToString());
            }
            else {
                return View(pvm);
            }

        }

    }
}

I am using the ViewModel pattern to send various bits of information to the View. 我正在使用ViewModel模式将各种信息发送到View。 For example, I am sending a Product with a default Id set to 5, and I am also setting a title property [Aside: this is not production code - just test data :-)] 例如,我发送的产品的默认ID设置为5,并且我还设置了title属性[此外:这不是生产代码-只是测试数据:-)]

All good so far. 到目前为止一切都很好。 When /Product/Create is called for the first time, my Title property is displayed on the view, and the Product Id defaults to 5. However, when I post the form, only the Product information is retained. 第一次调用/ Product / Create时,我的Title属性显示在视图上,并且Product ID默认为5。但是,当我发布表单时,仅保留Product信息。 If the ModelState is not Valid, I display the View to the user again. 如果ModelState无效,我将再次向用户显示视图。 However, this time around, the Title does not display (it is set to null). 但是,这一次,标题不显示(将其设置为null)。 The product does display as expected though. 产品确实按预期显示。

If possible I would like to send the original Title back to the view when the ModelState is not valid. 如果可能,我想在ModelState无效时将原始标题发送回视图。 Is there any way to do this without using Session, ViewData, and TempData? 没有使用Session,ViewData和TempData的方法,有什么方法吗?

Regards, and thanks in advance 问候,并提前感谢

If I understand you correctly, the short answer is "no." 如果我对您的理解正确,简短的回答是“否”。

If the ProductViewModel.TheTitle property isn't part of the form data posted to the Create POST method, you'll have to recreate it somehow . 如果ProductViewModel.TheTitle属性不是发布到Create POST方法的表单数据的一部分,则必须以某种方式重新创建它。 You're on track with the possible ways in which you could persist this value across multiple requests, but I'd ask whether it's really necessary to ultimately go there. 您正在设法在多个请求中保留此值的可能方式,但是我想问一问,最终是否真的有必要。

In my opinion, if the way in which you retrieve the Title property in the Create GET method is good enough for those requests, it's equally good enough to re-create it in the same way in the POST requests. 我认为,如果您在Create GET方法中检索Title属性的方式足以满足那些请求,则同样可以在POST请求中以相同的方式重新创建它。 A POST is typically going to take more resources and time to process (validating, saving data, etc.) as it is, so you're exposing yourself to extra dependencies and vulnerabilities for a likely trivial optimization. POST通常会按原样花费更多的资源和时间来处理(验证,保存数据等),因此您将自己暴露于额外的依赖关系和漏洞中,可能会进行微不足道的优化。 That said, I'm sure there are plenty of scenarios where you approach would be justified. 就是说,我敢肯定,在很多情况下您采取的做法都是合理的。

In your example, since the Title property isn't used to save the product and can effectively be ignored, if you still wanted to persist it across these pages without recreating it, the simplest way might be to include it in a hidden form field so your Model binder picks it up in addition to the Product. 在您的示例中,由于Title属性不用于保存产品并且可以被有效地忽略,因此,如果您仍然希望在不重新创建产品的情况下将其保留在这些页面上,则最简单的方法可能是将其包含在隐藏的表单字段中,因此除产品外,您的模型活页夹也可以将其拾取。 Just be aware end users could change this value if they manipulated the form payload. 请注意,如果最终用户操作了表单有效负载,则可以更改此值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM