C# 通过JQuery将MVC视图模型数据发布到控制器
我有以下型号:C# 通过JQuery将MVC视图模型数据发布到控制器,c#,jquery,asp.net-mvc,C#,Jquery,Asp.net Mvc,我有以下型号: public class ColourCounts { [Key] public int ColourGroup { get; set; } public int ColourCount { get; set; } public bool Selected { get; set; } } public class SizeCounts { [Key] public int SizeGroup { get; set; } pu
public class ColourCounts
{
[Key]
public int ColourGroup { get; set; }
public int ColourCount { get; set; }
public bool Selected { get; set; }
}
public class SizeCounts
{
[Key]
public int SizeGroup { get; set; }
public int SizeCount { get; set; }
public bool Selected { get; set; }
}
public class New
{
public New()
{
this.Newz = new New();
this.Colour = new ColourCounts();
this.ColourList = new List<ColourCounts>();
this.Size = new SizeCounts();
this.SizeList = new List<SizeCounts>();
}
public New Newz { get; set; }
public ColourCounts Colour { get; set; }
public List<ColourCounts> ColourList { get; set; }
public SizeCounts Size { get; set; }
public List<SizeCounts> SizeList { get; set; }
}
然后将使用Linq查询获得一些新计数,我将在部分视图中重新呈现EditorTemplates,与页面加载时一样,但使用新数据
但是,我无法以可以将表单数据传递给ReCalc控制器中的模型的方式捕获表单数据
我已经在jquery中使用
var x = $('#myForm').serializeArray();
var y = JSON.stringify(x);
而根据http://jsonlint.com/
我明白了
{"name":"SizeList[0].Selected","value":"true"},{"name":"SizeList[0].Selected","value":"false"}
很明显,这是一个问题,而且当在调试中查看时,控制器中的模型是空的
非常感谢您为jquery提供的帮助
如果您需要任何进一步的信息,请用外行的话告诉我,我是T-SQL的人:)谢谢
$.post('@Url.Action(“ReCalc”),$('#myForm').serialize(),函数(响应){……})代码>谢谢,现在好像可以用了!我确实尝试过var x=…serialize();但是不断得到一个无效的JASON原语错误。唯一的区别是我做了类似//$.ajax({//url://Newz/_-ReCalc)的事情,//dataType:'json',//contentType:'application/json;charset=utf-8',//type:'post',//cache:false,//data:x,并没有只使用$.post,而是查看了一下,它说这是一个缩写,我在我的一个中传递了类型或设置了吗?(出于我自己的学习目的,$.ajax()
如果您省略了contentType:'application/json;charset=utf-8'
$.post('@Url.Action(“ReCalc”),$('.#myForm').serialize(),function(response){……};
谢谢,现在似乎可以用了!我尝试过var x=…serialize;但不断收到一个无效的JASON基元错误。唯一的区别是我执行了类似于//$.ajax({//url://Newz/_-ReCalc)的操作,//dataType:'json',//contentType:'application/json;charset=utf-8',//type:'post',//cache:false,//data:x,并没有只使用$.post,而是查看了一下,它说这是一个缩写,我在我的一个中传递了类型或设置了吗?(出于我自己的学习目的,$.ajax()
如果您省略contentType:'application/json;charset=utf-8'
@model ...Models.New
<tbody>
<tr>
@Html.EditorFor(model => model.SizeList)
</tr>
</tbody>
@model ..Models.SizeCounts
<tr>
<td>
@Html.HiddenFor(m => m.SizeGroup)
@Html.DisplayFor(m => m.SizeGroup)
</td>
<td>
@Html.HiddenFor(m => m.SizeValue)
@Html.DisplayFor(m => m.SizeValue)
</td>
<td">
@Html.HiddenFor(m => m.SizeCount)
@Html.DisplayFor(m => m.SizeCount)
</td>
<td>@Html.CheckBoxFor(m => m.Selected)</td>
</tr>
@model ..Models.ColourCounts
<tr>
<td>
@Html.HiddenFor(m => m.ColourGroup)
@Html.DisplayFor(m => m.ColourGroup)
</td>
<td>
@Html.HiddenFor(m => m.ColourValue)
@Html.DisplayFor(m => m.ColourValue)
</td>
<td">
@Html.HiddenFor(m => m.ColourCount)
@Html.DisplayFor(m => m.ColourCount)
</td>
<td>@Html.CheckBoxFor(m => m.Selected)</td>
</tr>
<script type="text/javascript" name ="Size">
$(document).ready(function () {
//var val = $('#yearSelect3').val();
$.ajax({
url: "/News/_SizeCounts",
type: "GET",
})
.done(function (partialViewResult) {
$("#sizeTable").html(partialViewResult)
});
});
</script>
<script type="text/javascript" name="Colour">
$(document).ready(function () {
//var val = $('#yearSelect3').val();
$.ajax({
url: "/News/_ColourCounts",
type: "GET",
})
.done(function (partialViewResult) {
$("#colourTable").html(partialViewResult)
});
});
</script>
public ActionResult ReCalc(New model)
{
....
}
var x = $('#myForm').serializeArray();
var y = JSON.stringify(x);
{"name":"SizeList[0].Selected","value":"true"},{"name":"SizeList[0].Selected","value":"false"}