简体   繁体   中英

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

I have a Controller called 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. 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 :-)]

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. If the ModelState is not Valid, I display the View to the user again. However, this time around, the Title does not display (it is set to 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. Is there any way to do this without using Session, ViewData, and 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 . 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. 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. 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. Just be aware end users could change this value if they manipulated the form payload.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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