MVC Transfer Data Between Views

2019-02-19 17:14发布

I just started to learn MVC and am trying to understand how it works.

I don't want to send users to different views for all edit, insert and list operations.

In my sample application a View contains a list of items and below the list there is a form (for inserting new items) with action "{Controller}/Create" but there is no Create View.

When a user inserts a new item it posts to the Create action with httpverb post and creates the item and returns back to the List action with RedirectToAction method.

But I can not show any message(error, information etc) to the user in this style because I can not pass data between Create action and List action. How can I do that?

3条回答
劫难
2楼-- · 2019-02-19 17:37

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

查看更多
戒情不戒烟
3楼-- · 2019-02-19 17:43

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.

查看更多
欢心
4楼-- · 2019-02-19 17:48

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.

查看更多
登录 后发表回答