将视图模型列表传递给 mvc 控制器操作
Pass a list of view models to mvc controller action
我在将我的视图数据序列化到我的视图模型对象时遇到问题,并且不确定如何解决该问题。 scoreCards
调试时controller action为null
控制器动作:
public ActionResult SaveSkeetSinglesScores(SkeetSinglesScoreCards scoreCards) {
return View();
}
包含传入控制器的列表的视图模型 (SkeetSinglesScoreCards
):
public class SkeetSinglesScoreCards {
public List<SkeetSinglesScoreCard> scoreCards { get; set; }
}
个人SkeetSinglesScoreCard
视图模型:
public class SkeetSinglesScoreCard {
public string sgl_H_1 {get; set; }
public string sgl_L_1 { get; set; }
public string dbl_H_1 { get; set; }
public string dbl_L_1 { get; set; }
public string sgl_H_2 { get; set; }
public string sgl_L_2 { get; set; }
public string dbl_H_2 { get; set; }
public string dbl_L_2 { get; set; }
public string sgl_H_3 { get; set; }
public string sgl_L_3 { get; set; }
public string sgl_H_4 { get; set; }
public string sgl_L_4 { get; set; }
public string sgl_H_5 { get; set; }
public string sgl_L_5 { get; set; }
public string sgl_H_6 { get; set; }
public string sgl_L_6 { get; set; }
public string dbl_H_6 { get; set; }
public string dbl_L_6 { get; set; }
public string sgl_H_7 { get; set; }
public string sgl_L_7 { get; set; }
public string dbl_L_7 { get; set; }
public string dbl_H_7 { get; set; }
public string H_8 { get; set; }
public string L_8 { get; set; }
public string opt { get; set; }
}
最后是我将数据发布到控制器操作的视图:
@using (Html.BeginForm("SaveSkeetSinglesScores", "Scores", FormMethod.Post)) {
foreach (var round in Model) {
<table class="table table-bordered">
<thead>
<tr class="active">
<!--table headers here--!>
</tr>
</thead>
<tbody>
<tr>
<td>
<input name="@("scoreCards_"+i+"_sgl_H_1")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit1StationOneSinglesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_L_1")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit2StationOneSinglesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_H_1")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit3StationOneDoublesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_L_1")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit4StationOneDoublesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_H_2")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit5StationTwoSinglesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_L_2")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit6StationTwoSinglesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_H_2")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit7StationTwoDoublesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_L_2")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit8StationTwoDoublesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_H_3")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit9StationThreeSinglesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_L_3")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit10StationThreeSinglesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_H_4")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit11StationFourSinglesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_L_4")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit12StationFourSinglesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_H_5")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit13StationFiveSinglesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_L_5")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit14StationFiveSinglesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_H_6")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit15StationSixSinglesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_L_6")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit16StationSixSinglesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_H_6")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit17StationSixDoublesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_L_6")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit18StationSixDoublesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_H_7")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit19StationSevenSinglesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_L_7")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit20StationSevenSinglesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_L_7")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit21StationSevenDoublesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_H_7")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit22StationSevenDoublesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_H_8")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit23StationEightHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_L_8")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit24StationEightLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"opt")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.OptionScore)" />
</td>
<td class="text-center total">0</td>
@{i++;}
</tr>
</tbody>
</table>
}
<button class="btn btn-default" type="submit" value="Save">Save</button>
}
您当前的代码将生成名称类似于此模式的输入字段。
scoreCards_0_sgl_H_1
但是您的 HttpPost 操作方法接受一个 SkeetSinglesScoreCards
的对象,它有一个 scorecard
属性。所以你应该像这样生成 html 模型绑定才能工作。
<input name="scoreCards[0].sgl_H_1" value="somthing" />
<input name="scoreCards[1].sgl_H_1" value="somthing" />
这应该有效。
@using (Html.BeginForm())
{
var i = 0;
foreach (var skeetSinglesScoreCard in Model.scoreCards)
{
<input name="scoreCards[@i].sgl_H_1" type="text" value="someValue" />
i++;
}
<input type="submit" value="Submit form" />
}
当我想处理复杂的数据类型时,我必须管理必须传回控制器的数据集合,这就是我通常的处理方式:
1) 为 viewModel 创建一个 class(在本例中它将是您的 SkeetSinglesScoreCards)。
2) 在客户端代码 (HTML) 上,我将这些数据的 JSON 版本加载到隐藏输入中(有时 JSON 数据来自 viewModel class, 其他时候我在渲染页面时解析它,这取决于业务模型)。
3) 在客户端代码 (javascript) 上,我将 JSON 数据加载到 javascript 对象中。在此对象上考虑每个客户端操作。我什么时候可以完全避免通过 AJAX 向服务器重新发送信息,直到全局提交被 posted.
4) 当用户要求 post 他的操作结果时,我在 javascript 上捕获事件,将存储信息的 javascript 对象字符串化并 post 它到隐藏的输入。然后post表格。
5) 控制器签名需要一个class的元素,可以从隐藏输入中存储的数据结构中推断出来,参数名称与隐藏输入相同。
我发现这个方法很管用
我在将我的视图数据序列化到我的视图模型对象时遇到问题,并且不确定如何解决该问题。 scoreCards
调试时controller action为null
控制器动作:
public ActionResult SaveSkeetSinglesScores(SkeetSinglesScoreCards scoreCards) {
return View();
}
包含传入控制器的列表的视图模型 (SkeetSinglesScoreCards
):
public class SkeetSinglesScoreCards {
public List<SkeetSinglesScoreCard> scoreCards { get; set; }
}
个人SkeetSinglesScoreCard
视图模型:
public class SkeetSinglesScoreCard {
public string sgl_H_1 {get; set; }
public string sgl_L_1 { get; set; }
public string dbl_H_1 { get; set; }
public string dbl_L_1 { get; set; }
public string sgl_H_2 { get; set; }
public string sgl_L_2 { get; set; }
public string dbl_H_2 { get; set; }
public string dbl_L_2 { get; set; }
public string sgl_H_3 { get; set; }
public string sgl_L_3 { get; set; }
public string sgl_H_4 { get; set; }
public string sgl_L_4 { get; set; }
public string sgl_H_5 { get; set; }
public string sgl_L_5 { get; set; }
public string sgl_H_6 { get; set; }
public string sgl_L_6 { get; set; }
public string dbl_H_6 { get; set; }
public string dbl_L_6 { get; set; }
public string sgl_H_7 { get; set; }
public string sgl_L_7 { get; set; }
public string dbl_L_7 { get; set; }
public string dbl_H_7 { get; set; }
public string H_8 { get; set; }
public string L_8 { get; set; }
public string opt { get; set; }
}
最后是我将数据发布到控制器操作的视图:
@using (Html.BeginForm("SaveSkeetSinglesScores", "Scores", FormMethod.Post)) {
foreach (var round in Model) {
<table class="table table-bordered">
<thead>
<tr class="active">
<!--table headers here--!>
</tr>
</thead>
<tbody>
<tr>
<td>
<input name="@("scoreCards_"+i+"_sgl_H_1")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit1StationOneSinglesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_L_1")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit2StationOneSinglesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_H_1")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit3StationOneDoublesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_L_1")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit4StationOneDoublesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_H_2")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit5StationTwoSinglesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_L_2")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit6StationTwoSinglesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_H_2")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit7StationTwoDoublesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_L_2")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit8StationTwoDoublesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_H_3")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit9StationThreeSinglesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_L_3")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit10StationThreeSinglesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_H_4")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit11StationFourSinglesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_L_4")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit12StationFourSinglesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_H_5")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit13StationFiveSinglesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_L_5")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit14StationFiveSinglesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_H_6")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit15StationSixSinglesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_L_6")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit16StationSixSinglesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_H_6")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit17StationSixDoublesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_L_6")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit18StationSixDoublesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_H_7")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit19StationSevenSinglesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_sgl_L_7")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit20StationSevenSinglesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_L_7")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit21StationSevenDoublesLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_dbl_H_7")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit22StationSevenDoublesHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_H_8")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit23StationEightHigh)" />
</td>
<td>
<input name="@("scoreCards_"+i+"_L_8")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.Hit24StationEightLow)" />
</td>
<td>
<input name="@("scoreCards_"+i+"opt")" type="text" maxlength="1" size="1" value="@ScoreHitMisConverter.IsHitToTableRowValue(round.OptionScore)" />
</td>
<td class="text-center total">0</td>
@{i++;}
</tr>
</tbody>
</table>
}
<button class="btn btn-default" type="submit" value="Save">Save</button>
}
您当前的代码将生成名称类似于此模式的输入字段。
scoreCards_0_sgl_H_1
但是您的 HttpPost 操作方法接受一个 SkeetSinglesScoreCards
的对象,它有一个 scorecard
属性。所以你应该像这样生成 html 模型绑定才能工作。
<input name="scoreCards[0].sgl_H_1" value="somthing" />
<input name="scoreCards[1].sgl_H_1" value="somthing" />
这应该有效。
@using (Html.BeginForm())
{
var i = 0;
foreach (var skeetSinglesScoreCard in Model.scoreCards)
{
<input name="scoreCards[@i].sgl_H_1" type="text" value="someValue" />
i++;
}
<input type="submit" value="Submit form" />
}
当我想处理复杂的数据类型时,我必须管理必须传回控制器的数据集合,这就是我通常的处理方式:
1) 为 viewModel 创建一个 class(在本例中它将是您的 SkeetSinglesScoreCards)。
2) 在客户端代码 (HTML) 上,我将这些数据的 JSON 版本加载到隐藏输入中(有时 JSON 数据来自 viewModel class, 其他时候我在渲染页面时解析它,这取决于业务模型)。
3) 在客户端代码 (javascript) 上,我将 JSON 数据加载到 javascript 对象中。在此对象上考虑每个客户端操作。我什么时候可以完全避免通过 AJAX 向服务器重新发送信息,直到全局提交被 posted.
4) 当用户要求 post 他的操作结果时,我在 javascript 上捕获事件,将存储信息的 javascript 对象字符串化并 post 它到隐藏的输入。然后post表格。
5) 控制器签名需要一个class的元素,可以从隐藏输入中存储的数据结构中推断出来,参数名称与隐藏输入相同。
我发现这个方法很管用