将视图模型列表传递给mvc控制器操作



我在将视图的数据序列化到视图模型对象中时遇到问题,不知道如何解决该问题。控制器操作中的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>
}

您当前的代码将生成名称如下的输入字段。

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创建一个类(在这种情况下,它将是您的SkeetSinglesScoreCards)。

2) 在客户端代码(HTML)中,我将这些数据的JSON版本加载到隐藏的输入中(有时JSON数据来自viewModel类,其他时候我在呈现页面时解析它,这取决于业务模型)。

3) 在客户端代码(javascript)中,我将JSON数据加载到javascript对象中。每个客户端操作都会考虑到此对象。当我可以的时候,我完全避免通过AJAX向服务器重新发送信息,直到发布全局提交。

4) 当用户要求发布他的操作结果时,我在javascript上捕获事件,字符串化存储信息的javascript对象,并将其发布到隐藏的输入中。然后张贴表格。

5) 控制器签名期望类的元素可以从隐藏输入中存储的数据结构中推断出来,并且参数的名称与隐藏输入相同。

我发现这种方法工作得很好

相关内容

  • 没有找到相关文章

最新更新