Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The test aspx:

XML
<%@ 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:

SQL
public ActionResult test(int id)
    {
        if (Request.IsAjaxRequest())
        {


            TempData["page"] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            return PartialView("ArticleList", TempData["page"]);
        }
        return View();
    }


THe ArticleList partial view:

XML
<%@ 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
Updated 12-Mar-13 21:52pm
v3
Comments
Shubham Choudhary 12-Mar-13 4:08am    
where is your script manager add script manager!!!

H!!! take a look of this link

AJAX with ASP.NET MVC[^]
 
Share this answer
 
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

SQL
[HttpPost]
public ActionResult test(int id)
    {
        if (Request.IsAjaxRequest())
        {


            TempData["page"] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            return PartialView("ArticleList", ViewData["page"]);
        }
        return View();
    }
 
Share this answer
 
Comments
gavinv 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 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 Chintala 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900