Null Pointer
Null Pointer

Reputation: 9289

Problem with Html.RenderPartial -MVC

I am trying to update a small area in my web page.I just want to reload the ascx page(without refreshing the entire page) when user clicks the button. The code sample is shown below.When loading its work fine.

But after ajax request its not showing the loaded data(means name and address is changed). I can see in debugging the html with new data is constructing for ascx page after ajax requset. But its not updating the view .

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>

Controller

  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 />
 <% } %>

Model

    public string name { get; set; }
    public string address { get; set; }

Upvotes: 1

Views: 1377

Answers (1)

Amitabh
Amitabh

Reputation: 61167

You have to specify 'UpdateTargetId' (Html target to be updated). Try the following.

<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",
                           UpdateTargetId = "result"

                          }))

   { %><button type="submit" name="test">test button</button>

     <% } %>
        <div id="result"> 
        <% Html.RenderPartial("test"); %>      
       </div>  
</asp:Content>

Upvotes: 2

Related Questions