0

我有一个 ViewModel 定义为:

    public class Fixtures
    {
        public int Id { get; set; }
        public string HomeTeam { get; set; }
        public string AwayTeam { get; set; }
        public string HomeTeamCode { get; set; }
        public string AwayTeamCode { get; set; }
        public string League { get; set; }
        public bool Home { get; set; }
        public bool Draw { get; set; }
        public bool Away { get; set; }
        public string FixturePrediction { get; set; }
    }

我的视图显示了这些灯具的列表,对于每个灯具,我都有一个单选按钮分组。我需要某种方式将每个灯具的选定单选按钮传递给我的 Post 方法。我的视图和控制器如下:

看法:

<div class="container">
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        for (int i = 0; i < Model.Count(); i++)
        {
            @Html.HiddenFor(model => model[i].Id)

            <div class="col-sm-6">
                <div class="panel panel-primary panel-fixture">
                    <div class="panel-heading text-center fixturetitle">
                        @Html.DisplayFor(model => model[i].HomeTeam) v @Html.DisplayFor(model => model[i].AwayTeam)
                        @Html.HiddenFor(model => model[i].HomeTeam)
                        @Html.HiddenFor(model => model[i].AwayTeam)
                    </div>
                    <div class="panel-body">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-xs-4 text-center">
                                    <img src="~/Content/Images/@string.Concat(Model[i].HomeTeamCode, ".png")" class="fixturecrest" />
                                    <div class="text-center">
                                        @Html.DisplayFor(model => model[i].HomeTeamCode)
                                        @Html.HiddenFor(model => model[i].HomeTeamCode)
                                    </div>
                                </div>
                                <div class="col-xs-4 text-center">
                                    <label class="text-center">
                                        V <br />
                                    </label>
                                </div>
                                <div class="col-xs-4 text-center">
                                    <img src="~/Content/Images/@string.Concat(Model[i].AwayTeamCode, ".png")" class="fixturecrest" />
                                    <div class="text-center">
                                        @Html.DisplayFor(model => model[i].AwayTeamCode)
                                        @Html.HiddenFor(model => model[i].AwayTeamCode)
                                    </div>
                                </div>
                            </div>
                            <hr />
                            <div class="row">
                                <div class="text-center" role="group" aria-label="Fixture Prediction">
                                    <div class="col-xs-4 text-center">
                                        <button type="button" class="btn btn-xs btn-default">
                                            HOME
                                            @Html.RadioButtonFor(model => model[i].FixturePrediction, Model[i].Home, new { @Name = Model[i].Id })
                                            @Html.HiddenFor(model => model[i].FixturePrediction)
                                            <span class="glyphicon glyphicon-check"></span>
                                        </button>
                                    </div>
                                    <div class="col-xs-4 text-center">
                                        <button type="button" class="btn btn-xs btn-default">
                                            DRAW
                                            @Html.RadioButtonFor(model => model[i].FixturePrediction, Model[i].Draw, new { @Name = Model[i].Id })
                                            @Html.HiddenFor(model => model[i].FixturePrediction)
                                            <span class="glyphicon glyphicon-check"></span>
                                        </button>
                                    </div>
                                    <div class="col-xs-4 text-center">
                                        <button type="button" class="btn btn-xs btn-default">
                                            AWAY
                                            @Html.RadioButtonFor(model => model[i].FixturePrediction, Model[i].Away, new { @Name = Model[i].Id })
                                            @Html.HiddenFor(model => model[i].FixturePrediction)
                                            <span class="glyphicon glyphicon-check"></span>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        }
        <input type="submit" value="Confirm Predictions" class="btn btn-success" />
    }
</div>

控制器中的发布方法:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Test(IEnumerable<Fixtures> test)
{
    if (ModelState.IsValid)
    {

    }

    return RedirectToAction("Test");
}

我的字段FixturePrediction始终为空,并且我尝试了上面显示的“radiobuttonfor”的几种变体。

我需要的只是FixturePrediction根据选择的单选按钮分配一个值。我错过了什么?

提前致谢。

4

1 回答 1

1

我认为您要做的是,我不确定您为什么要按照自己的方式进行操作,但这就是您实现Html.RadioButtonFor的方式,它需要模型道具和值,您可以添加任意数量的按钮,选定的单选值将作为模型道具的值传递

@Html.RadioButtonFor(model => model[i].FixturePrediction, "Home")
 @Html.RadioButtonFor(model => model[i].FixturePrediction, "Draw")
 @Html.RadioButtonFor(model => model[i].FixturePrediction, "Away")

另一个例子

于 2018-04-23T16:33:52.697 回答