在 asp.net mvc2 中使用 json 到对象绑定

发布于 2024-12-23 03:45:24 字数 1566 浏览 3 评论 0原文

我试图将 JSON 字符串化对象绑定到模型和控制器中的一些其他字符串,但它不起作用。

难道不可能吗

$.ajax({
url: "/SrcManager/AddDataSource",
type: "POST",
data: JSON.stringify({
    content: ct,
    dataSourceName: $("#dataSrcName").val(),
    parameters: parametersCollection,
    sourceContentId: sourceContentId,
    sourceId: null,
    type: contType
}),
success: function (data) {
    if (data.length > 1)
    {
        alert("DataSource Saved Successfully");
        $("#dataSrcId").val(data);
    }
}
});

和 var 参数集合 = [];

函数 IPParameters(paramName, paramValue) { this.ParamName = paramName; this.ParamValue = paramValue; }

*** action method : public string AddDataSource(ContentModel scvm){.........}

上面的方法不起作用。这是不支持的还是代码中有任何错误,请建议正确的方法。

在 ContentModel 中,我将参数映射到 List

我还在 global.asax 中添加了 JsonValueProviderFactory

C# 模型是:

public class SourceContentViewModel
{
    public string sourceId { get; set; }

    public string dataSourceName { get; set; }

    public string sourceContentId { get; set; }

    public string content { get; set; }

    public string type { get; set; }
    public List<Parameters> parameters { get; set; }

    public SourceContentViewModel()
    {
        parameters = new List<Parameters>();
    }
}

public class Parameters
{
    public string ParamName { get; set; }
    public string ParamValue { get; set; }
}

I am trying to bind a JSON stringified object to a model and few other strings in the controller and it is not working.

is it not possible,

$.ajax({
url: "/SrcManager/AddDataSource",
type: "POST",
data: JSON.stringify({
    content: ct,
    dataSourceName: $("#dataSrcName").val(),
    parameters: parametersCollection,
    sourceContentId: sourceContentId,
    sourceId: null,
    type: contType
}),
success: function (data) {
    if (data.length > 1)
    {
        alert("DataSource Saved Successfully");
        $("#dataSrcId").val(data);
    }
}
});

and var parametersCollection = [];

function IPParameters(paramName, paramValue) {
this.ParamName = paramName;
this.ParamValue = paramValue; }

*** action method : public string AddDataSource(ContentModel scvm){.........}

Why does the above not work. Is this not supported or any mistake in the code, kindly suggest the right way.

In the ContentModel, i map the parameters to List<Parameters>.

I have added the JsonValueProviderFactory also in global.asax.

The C# model is :

public class SourceContentViewModel
{
    public string sourceId { get; set; }

    public string dataSourceName { get; set; }

    public string sourceContentId { get; set; }

    public string content { get; set; }

    public string type { get; set; }
    public List<Parameters> parameters { get; set; }

    public SourceContentViewModel()
    {
        parameters = new List<Parameters>();
    }
}

public class Parameters
{
    public string ParamName { get; set; }
    public string ParamValue { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的心境我的脸 2024-12-30 03:45:24

您无法将 JSON 发送到 ASP.NET MVC 2 应用程序,因为没有现成的 JSON 提供程序工厂允许您读取请求。它是在 ASP.NET MVC 3 中构建的。您可以查看 以下博客文章 并包含其中讨论的 JsonValueProviderFactory。然后,您将能够在注册后将 JSON 请求发送到 ASP.NET MVC 2 控制器操作:

protected void Application_Start() 
{
    RegisterRoutes(RouteTable.Routes);
    ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
}

另请注意 contentType: 'application/json;发送请求时的 charset=utf-8' 设置,指示绑定器您正在发送 JSON 请求。

You can't send JSON to an ASP.NET MVC 2 application as there is no JSON provider factory out-of-the-box that will allow you to read the request. It is built in ASP.NET MVC 3. You may take a look at the following blog post and include the JsonValueProviderFactory discussed there. Then you will be able to send JSON requests to your ASP.NET MVC 2 controller actions after registering it:

protected void Application_Start() 
{
    RegisterRoutes(RouteTable.Routes);
    ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
}

Also notice the contentType: 'application/json; charset=utf-8' setting when sending the request which instructs the binder that you are sending a JSON request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文