虽然有围绕这个话题的问题,但都没有充分回答。
问题:我有一个模型,大概有25个bool属性。用作复选框。我正在寻找一种方法来确定它们是否被选中。抓取该复选框的名称(或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; }
}
这很麻烦,也很慢。有没有更好的方法?有更好的方法来检查它们是否被选中吗?我必须把所有的内容都插入到一个DB中,作为true或false。一定感谢您的帮助。
你可以在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。家这个帮助。