【发布时间】:2018-09-04 12:51:57
【问题描述】:
我是 ASP.NET MVC 的新手。我做了一个局部视图,在其中动态显示了一个复选框。我的问题是如何获取复选框的值并将值发送到主控制器?
当我提交表单的值时,我不知道如何将值从局部视图传递到控制器。
这是我的控制器:
[ChildActionOnly]
public ActionResult ListaCheckVistaParcial()
{
return PartialView("ListaCheckVistaParcial", ListaCheckBox());
}
这是主视图:
@model InterfaceMonterrey.Models.UserAccount_RolAcceso
@{
ViewBag.Title = "Registrar";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Registrar</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>USUARIO</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Usuario_InterfaceMty.USERID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Usuario_InterfaceMty.USERID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Usuario_InterfaceMty.USERID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Usuario_InterfaceMty.NOMBRE, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Usuario_InterfaceMty.NOMBRE, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Usuario_InterfaceMty.NOMBRE, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Usuario_InterfaceMty.APELLIDO, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Usuario_InterfaceMty.APELLIDO, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Usuario_InterfaceMty.APELLIDO, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Usuario_InterfaceMty.USEREMAIL, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Usuario_InterfaceMty.USEREMAIL, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Usuario_InterfaceMty.USEREMAIL, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Usuario_InterfaceMty.PASSWORD, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Usuario_InterfaceMty.PASSWORD, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Usuario_InterfaceMty.PASSWORD, "", new { @class = "text-danger" })
</div>
</div>
@*<div class="form-group">
@Html.LabelFor(model => model.LOGIN_TIMESTAMP, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LOGIN_TIMESTAMP, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LOGIN_TIMESTAMP, "", new { @class = "text-danger" })
</div>
</div>*@
<div class="form-group">
@*@Html.LabelFor(model => model.PERFILID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PERFILID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PERFILID, "", new { @class = "text-danger" })
</div>*@
<div class="control-label col-md-2">
@Html.Label("PERFIL")
</div>
<div class="col-md-10">
@Html.DropDownListFor(m => m.Usuario_InterfaceMty.PERFILID, new SelectList(ViewBag.PerfilAcceso, "ID", "DESCRIPCION"), "SELECCIONE UN PERFIL", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Usuario_InterfaceMty.PERFILID, "", new { @class = "text-danger" })
</div>
</div>
@*<div class="form-group">
@Html.LabelFor(model => model.FECHAREGISTRO, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FECHAREGISTRO, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FECHAREGISTRO, "", new { @class = "text-danger" })
</div>
</div>*@
@*<div class="form-group">
@Html.LabelFor(model => model.ACTIVO, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ACTIVO, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ACTIVO, "", new { @class = "text-danger" })
</div>
</div>*@
<div class="form-group">
@Html.LabelFor(model => model.Usuario_InterfaceMty.ACTIVO, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.CheckBoxFor(m => m.Usuario_InterfaceMty.ActivoBool, htmlAttributes: new { @class = "null" })
@Html.ValidationMessageFor(model => model.Usuario_InterfaceMty.ACTIVO, "", new { @class = "text-danger" })
</div>
</div>
@*<h4>Asignar Rol</h4>
<hr />
<div class="form-group" style="width: 950px; height: 130px; overflow-y: scroll;">
@foreach (var item in (List<InterfaceMonterrey.Models.CheckBoxList>)ViewBag.ListaCB)
{
<div class="col-md-10">
@Html.Label(item.Nombre_ChB, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.CheckBoxFor(x => item.Value_Check, htmlAttributes: new { @class = "null" })
</div>
}
</div>*@
@Html.Action("ListaCheckVistaParcial")
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
这是我的部分观点:
del IEnumerable<InterfaceMonterrey.Models.CheckBoxList>
@using (Html.BeginForm())
{
@*@Html.AntiForgeryToken()*@
<div class="form-horizontal">
<h4>Asignar Rol</h4>
<hr />
<div class="form-group"style="width: 950px; height: 130px; overflow-y: scroll;">
@foreach (var item in Model)
{
<div class="col-md-10">
@Html.Label(item.Nombre_ChB, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.CheckBoxFor(x => item.Value_Check, htmlAttributes: new { @class = "null" })
</div>
}
</div>
</div>
}
【问题讨论】:
-
嗯,这是一个广泛的问题。您了解如何将其他数据从视图发布到控制器吗?您是否熟悉您在视图中使用的模型对象的概念以及数据将如何绑定到控制器目标方法上的对象?
-
拥有一个发布到所需控制器操作的表单将是显而易见的答案。
标签: asp.net-mvc checkbox asp.net-mvc-partialview