views:

28

answers:

3

Hi All,

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

I dont want to send user 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 user insert new item it posts to 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?

Thanks All.

A: 

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.

JacobM
A: 

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.

DOK
A: 

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. http://stackoverflow.com/questions/2333305/viewmodel-with-selectlist-binding-in-asp-net-mvc2/3073579#3073579


Let me know if you have any question.
-Soe

stun