C# 模型绑定复选框?
我一直使用MVC模型绑定,所以这对我来说是新的。我有一个班级和mvc表格C# 模型绑定复选框?,c#,asp.net-mvc-4,razor,model-binding,C#,Asp.net Mvc 4,Razor,Model Binding,我一直使用MVC模型绑定,所以这对我来说是新的。我有一个班级和mvc表格 public class Student { public string Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public bool? IsNew { get; set; } } 我的mvc razorpage <div class="f
public class Student
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool? IsNew { get; set; }
}
我的mvc razorpage
<div class="form-group">
<label class="checkbox-inline">
<input type="checkbox" name="isNew" id="isNew">Import
</label>
</div>
<div class="form-group">
<label for="firstName">FirstName</label>
<input class="form-control" name="firstName" id="firstName"/>
</div>
但是我还不能用同样的技术来测试checkbox?
我试着用
student.IsNew = request.Form.GetValues("isNew");
student.IsNew = request.Form.Get("isNew");
我怎样才能做到这一点呢?您应该使用MVC模型绑定,这样对您来说更容易 模型绑定读取所有发布的数据、查询字符串值等,并为您构建一个对象。模型绑定允许控制器代码与查询请求及其关联环境的肮脏行为保持干净的分离
public ActionResult Create()
{
var vm = new Student();
return View(vm);
}
[HttpPost]
public ActionResult Create(Student model)
{
//You can check model.IsNew and model.FirstName properties here
// TO DO : Save and Redirect to a success message action method
// Ex : return RedirectToAction("SavedSuccessfully");
}
以及您的强类型razor视图
@model Student
@using (Html.BeginForm())
{
<lable>FirstName </lable>@Html.TextBoxFor(d=>d.FirstName)
<lable>FirstName </lable>@Html.TextBoxFor(d => d.LastName)
<label>New ? </label>@Html.CheckBoxFor(g=>g.IsNew)
@Html.HiddenFor(d=>d.Id)
<p>
<input id="BtnAdd" name="myButton" type="submit" value="Add" />
</p>
}
@模范学生
@使用(Html.BeginForm())
{
FirstName@Html.TextBoxFor(d=>d.FirstName)
FirstName@Html.TextBoxFor(d=>d.LastName)
新建?@Html.CheckBoxFor(g=>g.IsNew)
@Html.HiddenFor(d=>d.Id)
}
早上好,
只是关于我如何解决此问题的更新:
我查看了表单中的值
我用这个来做模型绑定工作
if (!string.IsNullOrEmpty(request.Form.Get("isNew")))
{
vehicle.IsNew = true;
}
else
{
vehicle.IsNew = false;
}
作为将来的参考,在返回IList时始终使用mvc模型绑定,只需选择几个学生并将其传递给[HttpPost]public ActionResult学生(IFormCollection表单,int[]SelectedSID),以下是您可以在视图中添加的内容
@model IList<MvcDWOL.Data.Student>
@{
ViewData["Title"] = "Add Students";
}
@Html.Partial("_StatusMessage", @ViewData["StatusMessage"])
<div class="container"">
<div class="top-buffer"></div>
<div class="panel panel-primary" style="border:none">
<div class="panel-heading panel-head">Live</div>
<div class="panel-body">
@using (Html.BeginForm("AddEdit", "Student", FormMethod.Post))
{
<div class="form-actions no-color">
<p>
<input type="submit" value="Save" class="btn btn-primary"/>
</p>
</div>
<table>
<tbody>
<thead>
<tr>
<th>
Add (checkbox)
</th>
<th>
Name
</th>
<th>
Type
</th>
<th style="display:none">
Start Date
</th>
<th>
Start Time
</th>
<th style="display:none">
End Date
</th>
<th>
End Time
</th>
</tr>
</thead>
@if (Model.Any())
{
foreach (var item in Model)
{
<tr>
<th style="display:none">
<input id="item@(item.ID)" type="hidden"
name="SelectedSID"
value="@item.ID"
/>
</th>
<th>
<div style="zoom:1.5;">
@Html.CheckBox("IsAdded",@item.IsAdded)
</div>
</th>
<th>
@item.Name
<input id="item@(item.Name)" type="hidden" name="Name" value="@item.Name"/>
</th>
<th>
@item.Type.TypeName
<input id="item@(item.TypeID)" type="hidden" name="TypeID" value="@item.TypeID"/>
</th>
<th>
@item.StartTimeValue
<input id="item@(item.StartTime)" type="hidden" name="StartTime" value="@item.StartTime"/>
<input id="item@(item.StartTimeValue)" type="hidden" name="StartTimeValue" value="@item.StartTimeValue"/>
</th>
<th>
@item.EndTimeValue
<input id="item@(item.EndTime)" type="hidden" name="EndTime" value="@item.EndTime"/>
<input id="item@(item.EndTimeValue)" type="hidden" name="EndTimeValue" value="@item.EndTimeValue"/>
</th>
</tr>
}
}
</tbody>
</table>
@Html.ValidationSummary()
<div class="form-actions no-color">
<p>
<input type="submit" value="Save" class="btn btn-primary" />
</p>
</div>
}
</div></div></div>
@model-IList
@{
ViewData[“Title”]=“添加学生”;
}
@Html.Partial(“_StatusMessage”,@ViewData[“StatusMessage”])
如果您使用的是modelbinding,那么您的操作方法上不应该只有一个Student
参数吗?另请参见:以获得更好的razor页面语法,而不是绑定到属性。没有值的复选框在“
上发回,该复选框不能绑定到布尔属性。如果复选框未选中,则不会发布任何内容。为什么不使用模型绑定?我一直使用MVC模型绑定,但我正在为遗留代码调试这个问题。我可能不得不还原代码并使用MVC模型绑定。
@model IList<MvcDWOL.Data.Student>
@{
ViewData["Title"] = "Add Students";
}
@Html.Partial("_StatusMessage", @ViewData["StatusMessage"])
<div class="container"">
<div class="top-buffer"></div>
<div class="panel panel-primary" style="border:none">
<div class="panel-heading panel-head">Live</div>
<div class="panel-body">
@using (Html.BeginForm("AddEdit", "Student", FormMethod.Post))
{
<div class="form-actions no-color">
<p>
<input type="submit" value="Save" class="btn btn-primary"/>
</p>
</div>
<table>
<tbody>
<thead>
<tr>
<th>
Add (checkbox)
</th>
<th>
Name
</th>
<th>
Type
</th>
<th style="display:none">
Start Date
</th>
<th>
Start Time
</th>
<th style="display:none">
End Date
</th>
<th>
End Time
</th>
</tr>
</thead>
@if (Model.Any())
{
foreach (var item in Model)
{
<tr>
<th style="display:none">
<input id="item@(item.ID)" type="hidden"
name="SelectedSID"
value="@item.ID"
/>
</th>
<th>
<div style="zoom:1.5;">
@Html.CheckBox("IsAdded",@item.IsAdded)
</div>
</th>
<th>
@item.Name
<input id="item@(item.Name)" type="hidden" name="Name" value="@item.Name"/>
</th>
<th>
@item.Type.TypeName
<input id="item@(item.TypeID)" type="hidden" name="TypeID" value="@item.TypeID"/>
</th>
<th>
@item.StartTimeValue
<input id="item@(item.StartTime)" type="hidden" name="StartTime" value="@item.StartTime"/>
<input id="item@(item.StartTimeValue)" type="hidden" name="StartTimeValue" value="@item.StartTimeValue"/>
</th>
<th>
@item.EndTimeValue
<input id="item@(item.EndTime)" type="hidden" name="EndTime" value="@item.EndTime"/>
<input id="item@(item.EndTimeValue)" type="hidden" name="EndTimeValue" value="@item.EndTimeValue"/>
</th>
</tr>
}
}
</tbody>
</table>
@Html.ValidationSummary()
<div class="form-actions no-color">
<p>
<input type="submit" value="Save" class="btn btn-primary" />
</p>
</div>
}
</div></div></div>