Saeed-rz
Saeed-rz

Reputation: 1443

Ajax Call and return error in asp.net mvc3

i call an action controller in my view and when i set break point and watch the act of ajax function it work correctly on code behind but when back to function it not run 'success:function' and return error

$.ajax({
        type: "POST",
        url: "/GetListContents",
        data: { _PageID: PageID, _LangID: LangID, _BoxTypeID: 6, _ListTypeID: 8, _Count: CurPageIndex },
        cache: false,
        dataType: 'html',
        success: function (html) {
            $("#dynamicBody").html(html);
            $("#dynamicFooter").html("Hello");
        },
        error: function (xhRequest, ErrorText, thrownError) {
            $("#dynamicBody").html(xhRequest.status);
            $("#dynamicBody").html(ErrorText);
        }
    });

the GetListContents work on code behind but the result is error
tnx for advance
and sorry for poor english
Update
this is my action controller code

[HttpPost]
    public PartialViewResult GetListContents(int _PageID, int _LangID, int _BoxTypeID, int _ListTypeID, int _Count)
    {
        dynamic cOut = GetListContent(_PageID, _LangID, _BoxTypeID, _ListTypeID, _Count);
        return PartialView("DynamicPagingBoxInfo",null);
    }

    public dynamic GetListContent(int _PageID, int _LangID, int _BoxTypeID, int _ListTypeID, int _Count)
    {
        List<ipMedia_PageContentsCache> PageContent = new List<ipMedia_PageContentsCache>();
        ListPageDataProvider.GetDynamicBoxContent(ref PageContent, _PageID, _LangID,_Count * 10);
        ViewBag.PageListContents = PageContent;
        ViewBag.CurPageIndex = _Count;
        return PageContent;
    }

Update ||
more explain : my partial view use viewbag to render page and get data from code behind this partial view for first time that run on page request work fine but when i want render it with ajax,ajax function return error! my view bag has same structure in first time and in ajax call

Upvotes: 1

Views: 238

Answers (2)

Grinart
Grinart

Reputation: 286

may be an error is in line

url: "/GetListContents",

try use url: '@Url.Action('GetListContents','YourControllerName'), instead

Upvotes: 0

Eben Roux
Eben Roux

Reputation: 13256

You need to send back the actual html from your controller. Take a look at RenderPartial. I have used the following code to perform this function in the past (placed on a base controller that my controllers inherit from):

    protected string RenderPartialView(string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
        {
            viewName = ControllerContext.RouteData.GetRequiredString("action");
        }

        ViewData.Model = model;

        using (var sw = new StringWriter())
        {
            var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);

            viewResult.View.Render(viewContext, sw);

            return sw.GetStringBuilder().ToString();
        }
    }

Upvotes: 1

Related Questions