在我的网络应用程序中,我有一个动态生成的表单,我用它来创建一个JSON对象以回调到一个Action。如下所示:
function getConfigItemWithValidators() {
log.info("getConfigItemWithValidators()");
var oConfigItem = {
"Name": $("#txtName").html(),
"InputFileCellIndex": $("#inpFieldIndex").val(),
"Validators": new Array() };
for (var i = 0; true; i++) {
var oHiddenValidatorName = $("[name=hidVld"+i+"]");
var oHiddenValidatorVal = $("[name=txtVld"+i+"]");
if ($("[name=hidVld" + i + "]").length > 0) {
var oValidator = {
"ValidationType": oHiddenValidatorName.val(),
"ValidationValue": oHiddenValidatorVal.val() };
oConfigItem.Validators.push(oValidator);
}
else
break;
}
return oConfigItem
}
function saveConfigItemChanges() {
log.info("saveConfigItemChanges()");
var oConfigItem = getConfigItemWithValidators();
$("#divRulesContainer").hide("normal");
$.getJSON("PutValidationRules", oConfigItem,
saveConfigItemChangesCallback);
}
在我的操作中,在调试时,我注意到model.Validators为空:
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult PutValidationRules(ConfigItem model)
{
// model.Validators is empty
return Json(true);
}
以下是ConfigItem的代码:
public class ConfigItem
{
public string Name { get; set; }
public int InputFileCellIndex { get; set; }
private IList<Validator> _validators = new List<Validator>();
public IList<Validator> Validators
{
get
{
return _validators;
}
}
public void AddValidator(Validator aValidator)
{
aValidator.ConfigItem = this;
_validators.Add(aValidator);
}
}
我是否需要做些什么才能让我的JSON请求构建ConfigItem.Validators?