1
我想更新我的網頁中的一個小區域。我只是想重新加載ascx頁面(不刷新整個頁面)當用戶單擊按鈕。代碼示例如下所示。裝載工作正常。問題與Html.RenderPartial -MVC
但是,在ajax請求它沒有顯示加載的數據(意味着名稱和地址已更改)。我可以看到在調試新的數據的html正在爲ajax requset之後的ascx頁面構建。但它沒有更新視圖。
VIEW
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript" src="../../Scripts/MicrosoftAjax.js"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<% using (Ajax.BeginForm("Index", "Home",
new AjaxOptions {
HttpMethod="GET",
}))
{ %><button type="submit" name="test">test button</button>
<% } %>
<% Html.RenderPartial("test"); %>
</asp:Content>
控制器
public ActionResult Index()
{
Models.HomeModels obj = new Models.HomeModels();
obj.name = "initial name";
obj.address = "initial address";
if (Request.IsAjaxRequest())
{
obj.name="test";
obj.address = "success";
return PartialView("test",obj);
}
return View(obj);
}
Test.ascx
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<test_partial_renderning.Models.HomeModels>" %>
<% if(Model.address!=null)
{ %>
<%= Html.Encode(Model.name) %><br />
<%= Html.Encode(Model.address) %><br />
<% } %>
型號
public string name { get; set; }
public string address { get; set; }