开发者

MVC2 Problem refreshing View after Post action method

Basically all I am trying to do is have a View post back to it's own post action method. I want the action method to update a simple value and have the view then display the new value. Below is some code snippets. The post action method receives the value from the textbox well enough but after updating this value the view does not display the new value:

View:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div class="user_form" style="width: 600px; margin-top: 50px">
        <div class="user_form_header">
            Default Distribution List
        </div>        
        <% using (Html.BeginForm())
        { %>
            <div>
                <%= Html.TextBoxFor( model => model.CurrentPageIndex) %>
                <input type="submit" name开发者_JAVA百科="submitButton" value="Send" />
            </div>
        <% } %>
    </div>
</asp:Content>

Controller:

public ActionResult Index()
{
    DefaultDistributionListViewModel model = new DefaultDistributionListViewModel();
    model.CurrentPageIndex = 1;
    return View(model);
}

[HttpPost]
public ActionResult Index(DefaultDistributionListViewModel model, string submitButton)
{
    // repopulate the model
    model.CurrentPageIndex = model.CurrentPageIndex + 1;
    return View(model);
}

Controller:

public int CurrentPageIndex { get; set; }

Thanks,


In your HttpPost method, after you update the model.CurrentPageIndex and return View, Its gonna fire you HttpGet method where you reassign it.

model.CurrentPageIndex = 1;

Perhaps pass in a default parameter in your "Get" request.

// This sets a 'default' value as of c# 3 I believe // If not theres a [Default] attribute you can use as well as int? page

public ActionResult Index(int page = 1)
{
    var model = new DefaultDistributionListViewModel {
        CurrentPageIndex = page
    };
    return View(model);
}

[HttpPost]
public ActionResult Index(DefaultDistributionListViewModel model, string submitButton)
{
    // repopulate the model
    model.CurrentPageIndex = model.CurrentPageIndex + 1;
    return RedirectToAction("Index", new {page = model.CurrentPageIndex} );
}


It sounds like output caching is getting in the way. I would set up a cache profile and set it to not cache the view.

Here's how I do it.

Add to your web.config:

  <system.web>
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="CacheProfile" duration="60" varyByParam="*" />
          <add name="ZeroCacheProfile" duration="0" varyByParam="*" />
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
  </system.web>

Add the attribute to your controller:

[OutputCache(CacheProfile = "ZeroCacheProfile")]
public ActionResult Index(int page = 1) 
{ 
    var model = new DefaultDistributionListViewModel { 
        CurrentPageIndex = page 
    }; 
    return View(model); 
} 

This should fix your problem.

Rick

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜