PartialView not displaying refreshed values

Hello,

I am using ASP.NET 3.5 SP1 with MVC 1.

I have a PartialView (ascx) and a simple View which is using the PartialView. After submitting the form within ascx I want to refresh the PartialView using: return PartialView("MyPartialView", myViewModel).....but I am facing the following problems:

- The View is displayed with the old values and NOT the refreshed values.
- I loose the css (stylesheet) formatting when called return PartialView.

tech1guyAsked:
Who is Participating?
 
masterpassConnect With a Mentor Commented:
We had an issue some what similar like this , This is what saved us

http://stackoverflow.com/questions/677974/view-does-not-display-updated-values-asp-net-mvc
0
 
jamesbaileCommented:
Are you using AJAX to submit the form?

Maybe if you can post details of the view and controller action it will be easier to see what's going on.
0
 
tech1guyAuthor Commented:
I am NOT using using Ajax......but I can use jQuery1.3.2.

I'm attaching the code here as follows:

In the attached code you can see that I am facing 2 problems: PartialView is not displaying updatedViewModel and I'm loosing the style-sheet formatting.


VIEW

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.CustomerViewModel>" %>

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

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

    <div id="CustomerForm">
        <% Html.RenderPartial("CustomerForm"); %> 
    </div>

</asp:Content>


CONTROL
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication1.Models.CustomerViewModel>" %>


<% using (Html.BeginForm("SubmitAction", Url.RequestContext.RouteData.GetRequiredString("Controller"), FormMethod.Post))
{ %>
<table style="border:0px">
    <tr>
        <td valign="top">
            <div id="CustomerInformation">
                <table width="100%">
                    <tr class="headerRow">
                    <td colspan="2">Customer Information</td>
                    </tr>
                    <tr>
                        <td class="internalLeftColumn">Name</td>
                        <td class="internalRightColumn"><%= Html.TextBox("Name", Model.Name, new { @readonly = "readonly", @class = "inputDisable" })%></td>
                    </tr>
                    <tr>
                        <td class="internalLeftColumn">Address</td>
                        <td class="internalRightColumn"><%= Html.TextBox("Address", Model.Address, new { @readonly = "readonly", @class = "inputDisable" })%></td>
                    </tr>
                    <% for (int i = 0; i < Model.ListOfPhones.Count; i++)
                       {
                           %>
                            <tr>
                                <td>Phone <%= i.ToString() %>: </td>
                                <td><%= Html.TextBox("ListOfPhones[" + i + "].PhoneNumber", Model.ListOfPhones[i].PhoneNumber, new { @readonly = "readonly", @class = "inputDisable" })%></td>
                            </tr>
                    <% } %>
                </table>
            </div>
        </td>

    <tr>
        <tdalign="center">
            <input type="submit" name="button" value="SubmitAction" />
        </td>
    </tr>
  </table>
<% } %>

VIEWMODEL
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace MvcApplication1.Models
{
    public class CustomerViewModel
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public IList<Phone> ListOfPhones { get; set; }
    }

    public class Phone
    {
        public string PhoneNumber { get; set; }
        public string PhoneType { get; set; }
    }
}

CONTROLLER
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class CustomerController : Controller
    {
        public CustomerController()
        { }

        public ActionResult Index()
        {
            CustomerViewModel model = GetFakeCustomer();
            return View("CustomerView", model);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SubmitAction(CustomerViewModel model)
        {
            // After running standardized address changes, update the customer address
            model.Address = "Updated Address";

            return PartialView("CustomerForm", model); 
// Here I need to re-display the exact previous page with updated information. I do NOT want to use RedirectToAction or TempData.
        }

        private CustomerViewModel GetFakeCustomer()
        {
            CustomerViewModel model = new CustomerViewModel();
            model.Name = "Srinath";
            model.Address = "1654 Phase-5 SAS";
            model.ListOfPhones = new List<Phone>();

            Phone phone1 = new Phone();
            phone1.PhoneNumber = "123-123-1234";
            phone1.PhoneType = "Home";
            Phone phone2 = new Phone();
            phone2.PhoneNumber = "000-564-9871";
            phone2.PhoneType = "Cell";
            model.ListOfPhones.Add(phone1);
            model.ListOfPhones.Add(phone2);
            return model;
        }
    }
}

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
tech1guyAuthor Commented:
I forgot to mention that in real world this data is going to be secure and just want to confirm that if my existing method PartialView is not creating any problems in terms of security.
0
 
jamesbaileCommented:
A couple of things

Your form is using a standard (synchronous) POST. Therefore you need to return a full view therefore you need to change

return PartialView("CustomerForm", model);  

to

return View("CustomerView", model);  

Also whenever you do a POST you should not return the view direrectly, but perform a redirect to a GET to ensure that your browser is properly updated if the user refreshes the page etc.
See here for more dertails http://en.wikipedia.org/wiki/Post/Redirect/Get

therfore it should really be

return RedirectToAction("ShowUpdatedView");

and you should create an action

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult  ShowUpdatedView()
        {
            CustomerViewModel model = Get your updated model .........
            return View("CustomerView", model);
        }






0
 
tech1guyAuthor Commented:
I tired to use "return View("CustomerView", model)".............but still facing the following problems:

* It is still NOT displaying the refreshed values.

Secondly, the RedirectToAction thing, I'm more than willing to do this but the problem is how to "Get your updated model ........." within ShowUpdatedView()..........I don't want to use TempData because of security and performance and also don't have the access to any database so cannot store TempData in database etc....

my ONLY problem is "How to refresh the View with updated values as defined in the code"......could you please suggest any other way?
0
 
jamesbaileConnect With a Mentor Commented:
If you want your application to work correctly with your browser then you've got to use the PRG pattern and do a redirect to a GET after a sucessful POST.

When you say it's not displaying the refreshed values then you are either not returning the updated model in return View(.... or somthing is being cached somewhere.

On the second point it sounds like you've got a multi step process and you've got to store the state of the process somewhere - the choices are either server side (more secure) or client side (less secure). Server side you have many options some involving a database or local storage and others in the server memory. Client side you can use cookies or hidden form variables, but you've got to be aware of all the risks and potential issues which arise from this course of action. My recommendation would be to try and keep things server side.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.