在 asp.net mvc2 中使用 json 到对象绑定
我试图将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法将 JSON 发送到 ASP.NET MVC 2 应用程序,因为没有现成的 JSON 提供程序工厂允许您读取请求。它是在 ASP.NET MVC 3 中构建的。您可以查看 以下博客文章 并包含其中讨论的
JsonValueProviderFactory
。然后,您将能够在注册后将 JSON 请求发送到 ASP.NET MVC 2 控制器操作:另请注意
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: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.