首页 > 解决方案 > MVC 5 确定 CheckboxFor 是否被选中

问题描述

尽管围绕该主题存在一些问题,但没有人能充分回答它们。

问题:我有一个具有大约 25 个布尔属性的模型。用作复选框。我正在寻找一种方法来确定它们是否被检查。获取该复选框(或 ID)的名称以进行数据库输入。

我目前的方式似乎非常低效。我从 formcollection 中获取 Id 并有一些条件逻辑。

public ActionResult SubmitUwSelections(FormCollection form)
{
        var cbRating = form["cabRating"]; // Cab Raing ddl
        var filing = form["fileRqd"]; // filing required ddl
        var ifta = form["cbIfta"]; // IFTA cb
        var lossControl = form["lcRqd"]; // Loss control required cb
        var lossControlIf = form["lcInFile"]; // Loss control in file cb
        var app1 = form["AppInFile"]; //Application app in file cb
        var app2 = form["AppRqd"]; //Application app rqd cb
        var um1 = form["UMInFile"]; //UM in file cb
        var um2 = form["UMRqd"]; // UM rqd cb
        var terror1 = form["terrorInFile"]; // terror in file cb
        var terror2 = form["terrorRqd"]; // terror rqd cb
        var lossRun1 = form["LossRunInFile"];// loss run in file cb
        var lossRun2 = form["LossRunRqd"]; //Loss run rqd cb
        var inspect1 = form["cbVehicleInspectInFile"]; //vehicle inspect in file cb
        var inspect2 = form["cbVehicleInspectRqd"]; // vehicle inspect rqd cb
        var mvr1 = form["cbMvrInFile"]; // mvr in file cb
        var mvr2 = form["cbMvrRqd"]; // mvr rqd cb
        var loc1 = form["cbLocInFile"]; //Letter of Credit cb in file
        var loc2 = form["cbLocRqd"]; //Letter of Credit cb required
        var psComments = form["cifComments"]; //section 2 comment box
        var uniqPolicy = form["UniqPolicy"];
        var fileComplete = form["cbFileComplete"];


         //dto first section of FormCollection Form 
         if (ifta == null) { clDto.Ifta_Rqd = false; } else { clDto.Ifta_Rqd = true; }
         if (lossControl == null) { clDto.LossControl_Inspection_Rqd = false; } else { clDto.LossControl_Inspection_Rqd = true; }
         if (lossControlIf == null) { clDto.LcontrolInFile = false; } else { clDto.LcontrolInFile = true; }
         if (app1 == null) { clDto.ApplicationInFile = false; } else { clDto.ApplicationInFile = true; }
         if (app2 == null) { clDto.ApplicationRequired = false; } else { clDto.ApplicationRequired = true; }
         if (um1 == null) { clDto.UM_UimFormsInFile = false; } else { clDto.UM_UimFormsInFile = true; }
         if (um2 == null) { clDto.UM_UimFormsRqd = false; } else { clDto.UM_UimFormsRqd = true; }
         if (terror1 == null) { clDto.TerrorInFile = false; } else { clDto.TerrorInFile = true; }
         if (terror2 == null) { clDto.TerrorRqd = false; } else { clDto.TerrorRqd = true; }
         if (lossRun1 == null) { clDto.LossRunInFile = false; } else { clDto.LossRunInFile = true; }
         if (lossRun2 == null) { clDto.LossRunRqd = false; } else { clDto.LossRunRqd = true; }
         if (inspect1 == null) { clDto.vInspectionInFile = false; } else { clDto.vInspectionInFile = true; }
         if (inspect2 == null) { clDto.vInspectionRqd = false; } else { clDto.vInspectionRqd = true; }
         if (mvr1 == null) { clDto.MvrInFile = false; } else { clDto.MvrInFile = true; }
         if (mvr2 == null) { clDto.MvrRqd = false; } else { clDto.MvrRqd = true; }
         if (loc1 == null) { clDto.LocInFile = false; } else { clDto.LocInFile = true; }
         if (loc2 == null) { clDto.LocRqd = false; } else { clDto.LocRqd = true; }
         if (fileComplete == null) { clDto.FileComplete = false; } else { clDto.FileComplete = true; }
}

这很麻烦而且很慢。有一个更好的方法吗?检查它们是否被检查的更好方法?我必须将所有内容作为真或假插入数据库。非常感谢您的帮助。

标签: c#asp.net-mvcasp.net-mvc-5

解决方案


您可以在 MVC 中执行以下操作。我假设您有一个名为 UWSelectionsController 的控制器、2 个操作(Index 和 SubmitUwSelections)和一个我将称为 ChkBoxModel 的模型。所以你有你的模型

public partial class ChkBoxModel
{
    public bool cbRating { get; set; }
    public bool filing { get; set; }
    public bool ifta { get; set; }
    public bool lossControl { get; set; }
    public bool lossControlIf { get; set; }
    public bool app1 { get; set; }
    public bool app2 { get; set; }
    public bool um1 { get; set; }
    public bool um2 { get; set; }
    public bool terror1 { get; set; }
    public bool terror2 { get; set; }
    public bool lossRun1 { get; set; }
    public bool lossRun2 { get; set; }
    public bool inspect1 { get; set; }
    public bool inspect2 { get; set; }
    public bool mvr1 { get; set; }
    public bool mvr2 { get; set; }
    public bool loc1 { get; set; }
    public bool loc2 { get; set; }
    public bool psComments { get; set; }
    public bool uniqPolicy { get; set; }
    public bool fileComplete { get; set; }
}

您的控制器中有 2 个动作

public class UWSelectionsController : Controller
{
    public ActionResult Index()
    {
        return View(new ChkBoxModel()); ;
    }

    public ActionResult SubmitUwSelections(ChkBoxModel obj)
    {
        //access model bool (chkbox) parameter
    }
}

最后你有你的 index.cshtml 剃刀标记

@model WebApplication2.Controllers.ChkBoxModel

@using (Html.BeginForm("SubmitUwSelections", "Home", FormMethod.Post))
{
    @Html.DisplayNameFor(u => u.app1)
    @Html.CheckBoxFor(u => u.app1)
    <hr />
    @Html.DisplayNameFor(u => u.app2)
    @Html.CheckBoxFor(u => u.app2)

    //add the rest of the model ChkBoxModel properties here

    <hr />
    <button type="submit">Submit</button>
}

现在,当您提交此表格时,您不必检查

if (ifta == null) { clDto.Ifta_Rqd = false; } else { clDto.Ifta_Rqd = true; }

通过访问传递的模型,您会自动知道是否选择了某些东西,例如 obj.ifta 将被设置为 true 或 false。回家这有帮助。


推荐阅读