MVC Transfer Data Between Views

自古美人都是妖i 提交于 2019-12-01 11:02:04

Most MVC frameworks have the ability to temporarily store a small bit of data just through the next request, for just this purpose. In ASP.NET MVC its called TempData, in Rails it's called :flash, etc.

stun

You need to use Post Redirect Get PRG pattern.

Please read this Use PRG Pattern for Data Modification section in this blog post by Kazi Manzur Rashid.
http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx


This approach uses TempData to maintain ModelState data between redirects.

[HttpPost, ValidateAntiForgeryToken, ExportModelStateToTempData]
public ActionResult Create(FormCollection form)
{
    Product p = new Product();

    if (TryUpdateModel<IProductModel>(p))
    {
        productRepository.CreateProduct( p );
    }
    else
    {
        // add additional validation messages as needed
        ModelState.AddModelError("_generic", "Error Msg");
    }

    return RedirectToAction("Index");
}


And here is your Index action method.

[ImportModelStateFromTempData]
public ActionResult Index()
{
    IList<Product> products = productRepository.GetAll();
    return View("Index", products);
}


And here is your Index view.

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IList<Product>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Products</h2>

    <% foreach (var p in Model) { %>
        <div><%= Html.Encode( p.ProductName ) %></div>
    <% } %>

    <%= Html.ValidationSummary("Please correct the errors", new { id = "valSumCreateForm" }) %>
    <% using (Html.BeginForm("Create", "Product")) { %>
        Product Name: <%= Html.TextBox("ProductName") %>
        <%= Html.AntiForgeryToken() %>
        <% ViewContext.FormContext.ValidationSummaryId = "valSumCreateForm"; %>
    <% } %>
</asp:Content>
  1. The ImportModelStateFromTempData and ExportModelStateToTempData attributes helps transfer model state errors between redirects. This
  2. <% ViewContext.FormContext.ValidationSummaryId = "valSumCreateForm"; %> associates the MVC Form with its corresponding Validation Summary.



You can check another answer by me on this here as well. ViewModel with SelectList binding in ASP.NET MVC2


Let me know if you have any question.
-Soe

This article explains how to use TempData:

One of the more annoying things to deal with in Web programming is errors on a form. More specifically, you want to display error messages, but you want to keep the previously entered data. We've all had the experience of making a mistake on a form that has 35 fields, only to be presented with a bunch of error messages and a new, blank form. The MVC Framework offers TempData as a place to store the previously entered information so that the form can be repopulated. This is something that ViewState actually made very easy in Web Forms, since saving the contents of controls was pretty much automatic. ... TempData is a dictionary, much like the untyped ViewData. However, the contents of TempData only live for a single request and then they're deleted.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!