Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Ajax MVC partialview
The test aspx:
 
<%@ Page Title="test" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="MvcBBS.Models" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    test
</asp:Content>
<asp:Content ID="ChildHead" ContentPlaceHolderID="HeadContent" runat="server">
 
    <script type="text/javascript">
        function spanclick() {
            var e = e || window.event; obj = e.srcElement || e.target;
            if (obj.id != null) {
 
                document.getElementById("now").innerHTML = obj.id;
            }
 

            $.ajax({
                type: "POST",
                url: '@Url.Action("test", "Article")',
                data: { id: 1 },
                datatype: "html",
 
                success: function (evt) {
 
                    $('#divArticleList').html(evt);
                },
                error: function (err, errMsg, obj) { alert(errMsg) }
 
            });
        }
 

 
    </script>
 
    <style type="text/css">
        h2
        {
            margin: 0;
            padding: 0;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
   <fieldset id="fset">
<br />
  <legend> <span id="now" class="userb">AAAAAAAAA:</span></legend>
<br />
 

 
    <div id="divArticleList">
        <%Html.RenderPartial("~/Views/Shared/ArticleList.ascx", ViewData.Model);%>
        <span id="Span1" onclick="spanclick()" >bbbbbbbbbbbbbbbbbbbbbbbbbb</span>
    </div>
    </fieldset>
</asp:Content>
 
The test action:
 
public ActionResult test(int id)
    {
        if (Request.IsAjaxRequest())
        {
 

            TempData["page"] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            return PartialView("ArticleList", TempData["page"]);
        }
        return View();
    }
 
THe ArticleList partial view:
 
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%@ Import Namespace="MvcBBS.Models" %>
 
 <div id="page" class="page">&nbsp;&nbsp;<%=TempData["page"]%> </div>
 
i'm beginer in ajax,so somebody do help me please!!
Posted 11-Mar-13 22:31pm
gavinv421
Edited 12-Mar-13 22:52pm
v3
Comments
Shubham Choudhary at 12-Mar-13 4:08am
   
where is your script manager add script manager!!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

H!!! take a look of this link
 
AJAX with ASP.NET MVC[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The problem is that you have used $.ajax with an option type as POST and you have not decorated your action with POST.
 
So the solution is
 
[HttpPost]
public ActionResult test(int id)
    {
        if (Request.IsAjaxRequest())
        {
 

            TempData["page"] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            return PartialView("ArticleList", ViewData["page"]);
        }
        return View();
    }
  Permalink  
Comments
gavinv at 13-Mar-13 3:56am
   
thank for your answer,it's my mistake for typing "TempData" inot "ViewData",i've colleted it,and the problem still beeing.
i chang the code as bellow, then it's just show the "alert"
 
function spanclick() {
var e = e || window.event; obj = e.srcElement || e.target;
if (obj.id != null) {
 
document.getElementById("now").innerHTML = obj.id;
}
 

$.ajax({
type: "POST",
url: '@Url.Action("test","Article")',
data: { id: 1 },
datatype: "html",

success: function (data) {
 
alert("errMsg");
}
});
}
gavinv at 13-Mar-13 4:01am
   
i make mistake again,it don't show the the "alert" anyway. I think this means ajax don't get the return from controller, that's right?
Karthik Reddy at 13-Mar-13 4:54am
   
Also you need to change option in your ajax. You have an option called "datatype" in your ajax call change it to "dataType:"html"". You are returning your action result to a view Its neither a html nor JSON. so your success call back will never be called

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 274
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 PIEBALDconsult 200
4 BillWoodruff 174
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 13 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100