我有一个看起来像这样的用户界面:
我正在尝试将新创建的行的数据发送到服务器,以便服务器可以保存它。
我正在将JSON格式的数据从客户端发送到我的MVC应用程序。这是我的ajax请求:
var values = []; // an array with each item being an object/associative array
// more code to get values into variables
for (var i = 0; i < cultures.length; i++) {
var cultureName = cultures[i];
var valueTextBox = $(row).find(...);
var value = $(valueTextBox).val();
var cultureNameAndValue = { 'CultureShortName' : cultureName, 'StringValue' : value };
values.push(cultureNameAndValue);
}
var stringTableRow =
{
'ResourceKeyId': resourceKeyId,
'Key': resourceKeyName,
'CategoryId': categoryId,
'CategoryName': categoryName,
'StringValues': values
};
var stringified = JSON.stringify({ StringTableRow: stringTableRow });
$.ajax('/Strings/JsonCreateNew',
{
cache: false,
async: false,
type: 'POST',
contentType: 'application/json; charset=UTF-8',
data: stringified,
dataType: 'json',
error: SaveNewResourceClientSideHandler.OnError,
success: SaveNewResourceClientSideHandler.OnSuccess
});
这是它发送的数据(如Firebug中所示):
{"StringTableRow":{"ResourceKeyId":"","Key":"Foo",
"CategoryId":"1","CategoryName":"JavaScript",
"StringValues":[
{"CultureShortName":"en-US","StringValue":"Something"},
{"CultureShortName":"fr-FR","StringValue":""}]
}}
这是我的服务器端代码:
public ActionResult JsonCreateNew(StringTableRow row)
{
// CreateNewStringTableRow(row);
// return a success code, new resource key id, new category id
// return Json(
new { Success = true, ResourceKeyId = row.ResourceKeyId,
CategoryId = row.CategoryId },
JsonRequestBehavior.AllowGet);
return new EmptyResult();
}
这是我希望将传入的POST数据绑定到的业务对象:
public class StringTableRow
{
public StringTableRow()
{
StringValues = new List<CultureNameAndStringValue>();
}
public long ResourceKeyId { get; set; }
public string Key { get; set; }
public long CategoryId { get; set; }
public string CategoryName { get; set; }
public IList<CultureNameAndStringValue> StringValues { get; set; }
}
public class CultureNameAndStringValue
{
public string CultureShortName { get; set; }
public string StringValue { get; set; }
}
Global.asax
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
}
}
问题:
该操作JsonCreateNew
接收到一个不为null的对象,但所有属性都未初始化,即所有可为空的属性均为null,而value属性为默认值。因此,即使客户端发送一个完全有效的Json字符串,我实际上也不会得到任何数据。
我需要进行自定义模型绑定吗?
好的,我解决了我的问题,这对于其他程序员也可能是重要的见解,因为我在解决自己的问题的同时学到了一些东西。
我的解决方案:我不使用JSON,而是使用HTTP用于编码表单发布值的默认编码,然后使用了一些自定义模型绑定的东西(实际上没有创建模型绑定器)。
说明:通常,当您发出ajax请求并将数据从客户端传递到任何服务器平台时,如果您未指定编码,即contentType
jQuery的ajax方法的settings对象中的参数(或者如果您使用其他任何方法来发出除jQuery之外的ajax请求,然后设置ContentType
HTTP标头就是您要执行的操作),HTTP使用其默认编码对发布的数据进行编码,这与您在查询字符串后使用GET请求发布的内容非常相似,只有它是二进制编码的,并且不作为URL的一部分发送。
如果是这样的话,而你发布的任何类型的(集合IList
,IEnumerable
,ICollection
,IDictionary
,等),ASP.NET MVC,那么就不要在JavaScript中创建关联数组来体现的集合。甚至不创建数组。不要创建任何东西。
实际上,只需使用约定将其沿主要大对象传递:
data =
{ /*scalar property */ Gar: 'har',
/* collection */ 'Foo[index++].Bar' : 'value1',
'Foo[index++].Bar' : 'value2'
}
而且不要使用JSON。这将解决您一半的问题。
在服务器端,以接收发布的数据,FormCollection
并使用其IValueProvider
方法(GetValue
)来挖掘所需的数据。您不必显式创建模型绑定程序即可。您可以使用控制器本身的私有方法来执行此操作。
我会在有更多时间的时候再改善此答案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句