【发布时间】:2016-07-24 06:57:49
【问题描述】:
我在将视图数据序列化到视图模型对象时遇到问题,但不确定如何解决该问题。调试时控制器动作中的scoreCards为空。
控制器动作:
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>
}
【问题讨论】:
-
对于这种情况,我发现将视图模型转换为 JSON 并将其作为字符串直接传递给控制器(例如,输入字段)通常是一个好主意。模型绑定似乎比复杂的 IEnumerable 数据类型更能解析 JSON 字符串。
-
@Bardo 仍然对如何解决问题有些困惑。你能就如何解决这个问题提供这个上下文的答案吗?
标签: c# asp.net-mvc razor viewmodel