ASP.NET MVC - 使用AJAX渲染PartialView?

时间:2022-11-30 17:43:07

Earlier today I posted another post where @Darin Dimitrov helped me great, however once again I'm stucked... My javascript calls the AddWebsite ActionResult which does it job as it should, however the error function in the $.ajax() is always firing since

今天早些时候我发布了另一篇帖子,其中@Darin Dimitrov帮助了我很棒,但是我又一次被卡住......我的javascript调用了AddWebsite ActionResult,它可以正常工作,但$ .ajax()中的错误函数是从那以后总是开火

return PartialView(ListPartialView, MapUserToViewModel); 

isn't valid JSON.

是无效的JSON。

I've come across examples where people use something like

我遇到过人们使用类似内容的例子

RenderPartialViewToString(partialview, model);

and throws it into a JSON object... but it's just too "hackish" if you ask me.. isn't there an easier way to accomplish this?

并把它扔进一个JSON对象......但是如果你问我那就太“黑”了......是不是有更简单的方法来完成这个?

... And the code:

......代码:

// DashboardController.cs

[HttpPost]
public ActionResult AddWebsite(CreateWebsiteViewModel website)
{
    if (!ModelState.IsValid)
    {
        throw new HttpException(400, "Client-side validation failed.");
    }

    if (string.IsNullOrWhiteSpace(website.URL))
    {
        throw new ArgumentNullException("URL", "The URL cannot be empty nor contain only whitespaces.");
    }

    using (_session.BeginTransaction())
    {
        _session.Query(new AddWebsite(_contextProvider.GetUserSession.UserId, website.URL));
        _session.Transaction.Commit();
    }

    return PartialView(ListPartialView, MapUserToViewModel);
}

// MyJs.js

$("#testform").live('submit', function () {

    var test = { URL: $("#URL").val() };

        $.ajax({
            url: "/Dashboard/AddWebsite",
            type: "POST",
            data: JSON.stringify(test),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                alert("TRIG");
                $("#content").html(data);
            },
            error: function () {
                alert("Error");
            }
        });

    return false;
});

Thanks in advance!

提前致谢!

2 个解决方案

#1


4  

In your particular instance I think the problem is with your javascript code. You are specifying the dataType (which is what the function expects to parse in the response) as json. Based on the Action you posted you should have html as the dataType and it should solve your problem. There's nothing wrong with doing that (you don't have to use JSON for everything).

在您的特定实例中,我认为问题在于您的JavaScript代码。您将指定dataType(函数期望在响应中解析)作为json。基于您发布的Action,您应该将html作为dataType,它应该可以解决您的问题。这样做没有任何问题(你不必为所有事情使用JSON)。

#2


1  

Simple data

Why are you setting dataType and contentType in the first place? Since your object test is very simple you can just provide it as is and it will be consumed by Asp.net MVC without any problems and you will return your partial view.

为什么要首先设置dataType和contentType?由于您的对象测试非常简单,您可以按原样提供它,它将被Asp.net MVC使用而没有任何问题,您将返回您的局部视图。

Complex data

If your object would be more complex then you could use a different jQuery plugin that will make it possible to send complex JSON objects without strigification.

如果你的对象会更复杂,那么你可以使用一个不同的jQuery插件,这样就可以在没有条件化的情况下发送复杂的JSON对象。

#1


4  

In your particular instance I think the problem is with your javascript code. You are specifying the dataType (which is what the function expects to parse in the response) as json. Based on the Action you posted you should have html as the dataType and it should solve your problem. There's nothing wrong with doing that (you don't have to use JSON for everything).

在您的特定实例中,我认为问题在于您的JavaScript代码。您将指定dataType(函数期望在响应中解析)作为json。基于您发布的Action,您应该将html作为dataType,它应该可以解决您的问题。这样做没有任何问题(你不必为所有事情使用JSON)。

#2


1  

Simple data

Why are you setting dataType and contentType in the first place? Since your object test is very simple you can just provide it as is and it will be consumed by Asp.net MVC without any problems and you will return your partial view.

为什么要首先设置dataType和contentType?由于您的对象测试非常简单,您可以按原样提供它,它将被Asp.net MVC使用而没有任何问题,您将返回您的局部视图。

Complex data

If your object would be more complex then you could use a different jQuery plugin that will make it possible to send complex JSON objects without strigification.

如果你的对象会更复杂,那么你可以使用一个不同的jQuery插件,这样就可以在没有条件化的情况下发送复杂的JSON对象。