我有一个模型,我发送给我的观点。由于这个模型中有几个复杂的对象,所以我使用var model = <%= Html.Raw(Json.Encode(Model)) %>;将模型作为全局变量向所有jQuery方法公开。
在编辑完模型(创建新项、更新现有项和将对象从一个位置更改到另一个位置)之后,我使用以下代码将其发布到控制器:
$("#Update").click(function () {
$.ajax({
url: '<%= Url.Action("Update", "Editor") %>',
type: "POST",
dataType: 'json',
data: JSON.stringify({aModel: model}),
success: function (data) {
}
}).done(function (result) {
});
});在我的主计长那里接收它:
[HttpPost]
public ActionResult Update(MyModel aModel)
{
ModelState.Clear()
aModel.Execute();
return View("path/to/view");
}当我检查JSON.stringify({aModel: model})的结果时,我可以看到我的更改。但是,对象aModel没有接收到这种更改。
我试图清除ModelState,我试图避免缓存,我尝试从模型中复制一个存在的元素并更改它的值,而不是创建一个新的项来推送到Json数组,但是这些都没有起作用。
我的模型如下:
public class MyModel
{
public Dictionary<string, List<Item>> Items { get; set; }
public List<ItemProperty> Properties { get; set; }
}“词典”会不会导致模特儿招投标问题?假设这是一个模型投标问题。
有什么想法吗?
编辑1
根据评论,我认为问题可能在复杂类型Item和ItemProperty中。我试图对Id属性使用无参数构造函数和公共设置器,但它似乎没有解决这个问题。
以下是课程:
public class Item
{
public const string CONST = "Value";
private SubItem aSubItem;
public List<SubItem> SubItems { get ; set; }
public string Id { get; private set; }
public string Name { get; set; }
public string Help { get; set; }
public DateTime Time { get; set; }
public Item(string anId)
{
Id = anId;
}
}
public class SubItem
{
public List<ItemProperty> Properties = new List<ItemProperty>();
public string Id { get; private set; }
public string Subtitle { get; set; }
public bool SubtitleVisible { get; set; }
public bool Visible { get; set; }
public MyEnumType Type { get; set; }
public SubItem(string anId)
{
Id = anId;
}
}
public class ItemProperty
{
public string Id { get; private set; }
public string Title { get; set; }
public string Path { get; set; }
public bool Visible { get; set; }
public MyAction CustomAction = new MyAction();
public ItemProperty(string anId)
{
Id = anId;
}
}
public class MyAction
{
public string ActionName { get; set; }
}以及发布时的序列化模型。与原始模型相比,我唯一更改的是Name属性:
"{"Items":
{"ITEM1":
[{"SubItems":
[{"Properties":[],
"Subtitle":"Other",
"SubtitleVisible":true,
"Id":"Other",
"Visible":true,
"MyEnumType":0}
],
"Help":"",
"Name":"ADDED A NAME TO SEND TO THE CONTROLLER",
"Id":"ITEM1",
"Time":"/Date(-62135578800000)/"},
...发布于 2015-03-16 14:36:10
那是因为你在发一个字符串。它看起来像JSON这一事实毫无意义。发布实际的对象。默认情况下,jQuery将对其进行编码并以可以由您的操作解析的格式发送。当您现在处理它时,您只是张贴一个字符串,模型绑定程序试图绑定到一个MyModel实例,但是因为MyModel是类类型,而不是字符串类型,所以它失败了。
$.ajax({
url: '<%= Url.Action("Update", "Editor") %>',
type: "POST",
dataType: 'json',
data: {aModel: model},
success: function (data) {
}
}).done(function (result) {
});https://stackoverflow.com/questions/29079140
复制相似问题