c# - 将 View 模型列表传递给 mvc Controller 操作

标签 c# asp.net-mvc razor viewmodel

我在将我的 View 数据序列化到我的 View 模型对象时遇到问题,并且不确定如何解决该问题。调试时, Controller 操作中的 scoreCards 为 null。

Controller Action :

public ActionResult SaveSkeetSinglesScores(SkeetSinglesScoreCards scoreCards) {
        return View();
    }

包含传递到 Controller 的列表的 View 模型(SkeetSinglesScoreCards):

public class SkeetSinglesScoreCards {
    public List<SkeetSinglesScoreCard> scoreCards { get; set; }
}

个人 SkeetSinglesScoreCard View 模型:

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; }
}

最后是我将数据发布到 Controller 操作的 View :

@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 以使模型绑定(bind)正常工作。

<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" />
}

关于c# - 将 View 模型列表传递给 mvc Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36407659/

相关文章:

asp.net-mvc-3 - 从另一个文件指定布局会产生编译错误(ASP.NET MVC3/Razor)

css - MVC jquery mobile 在加载时不占用整个页面宽度

c# - 没有路由与提供的值匹配

c# - 从控制台应用程序中的另一个类调用 appsettings.json 数据

c# - 内部文本中具有特殊字符的 XmlElement

jquery - 对话框 Jquery 不工作

基类中的 C# 泛型类型

c# - 使用 Dijkstra 算法实现时间表

asp.net-mvc - ASP.NET MVC ViewModel 良好实践

javascript - 使用 JavaScript 和 MVC/Razor 将 Kendo 控件绑定(bind)到 HtmlHelper 控件