【问题标题】:Pass the selected checkbox value from view to controller将选中的复选框值从视图传递到控制器
【发布时间】:2013-03-14 12:54:49
【问题描述】:

我有这样的看法:

@model SCWW.Areas.OnlineBookings.Models.Updates.UpdateDetailsModel

@using (Html.BeginForm("Apply", "Updates", new { area = "OnlineBookings", consignmentKey = Model.ConsignmentKey }, FormMethod.Post, new { id = "updateableForm" }))
{
<fieldset>
    <legend>Updateable - @Html.DisplayFor(model => model.ConsignmentKey)</legend>
    <div class="well well-small ">
        @Html.HiddenFor(m => m.ConsignmentKey)

        <table id="updateDetails" class="table table-bordered table-striped table-hover dataTable">
            <tbody>
                <tr>
                    <td>Completed Date</td>
                    <td>@Html.CheckBoxFor(m => m.Apply)</td>
                    <td class="table-input">@Html.TextBoxFor(m => m.CompletedDate, new { Disabled = "Disabled" })</td>
                </tr>
                <tr>
                    <td>Cancelled Date</td>
                    <td>@Html.CheckBoxFor(m => m.Apply)</td>
                    <td class="table-input">@Html.TextBoxFor(m => m.CancelDate, new { Disabled = "Disabled" })</td>
                </tr>
                <tr>
                    <td>Booking Number</td>
                    <td>@Html.CheckBoxFor(m => m.Apply)</td>
                    <td class="table-input">@Html.TextBoxFor(m => m.BookingNumber, new { Disabled = "Disabled" })</td>
                </tr>

            </tbody>
        </table>
        <button type="submit" id="applyProxy" name="action" value="@FormAction.Apply" class="btn btn-success">Update</button>
    </div>

</fieldset>

}

控制器:

 [HttpPost]
    public ActionResult Apply(UpdateDetailsModel model, FormAction action)
    {
        if (!ModelState.IsValid)
        {
            return View("Submit",     GenerateViewModel(model.ConsignmentKey));
        }

        updateableService.Update(model.ConsignmentKey,"CompletedDate", model.ToDto());

        return RedirectToActionWithHash("Details", "otherActionsTab", "Bookings",
                                       new { consignmentKey = model.ConsignmentKey });
    }

在我看来,有 3 行,每行有一个复选框。当我单击视图上的“更新”按钮时,只选择了一个复选框。 我如何知道选择了哪个复选框?如何将选中的复选框值从视图传递到控制器 - 控制器中的 Apply(UpdateDetailsModel model, FormAction action)

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    您使用相同的模型属性 3 次。您需要为每个条件创建单独的属性。

    【讨论】:

    • 除了使用 3 个单独的属性之外,还有其他方法吗?请。
    • 没有什么是不模糊的。您可以根据其中输入的文本框进行猜测,但是一旦用户将输入输入 > 1 个文本框,即使他们只选择一个选项,它也会中断。您需要 3 个属性来选择 3 个选项。
    猜你喜欢
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多