[英]using json to object binding in asp.net mvc2
我試圖將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 parametersCollection = [];
function IPParameters(paramName,paramValue){this.ParamName = paramName; this.ParamValue = paramValue; }
*** action method : public string AddDataSource(ContentModel scvm){.........}
為什么以上不起作用。 這是不支持或代碼中的任何錯誤,請提出正確的方法。
在ContentModel中,我將參數映射到List<Parameters>
。
我在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; }
}
您無法將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'
發送請求時的contentType: 'application/json; charset=utf-8'
設置,該請求指示綁定器您正在發送JSON請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.