1

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; }

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.