我是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>
}
答案 0 :(得分:1)
无需在局部视图中使用Html.BeginForm()
,因为局部视图已经在主视图的Html.BeginForm()
内部呈现。因此,编写您的局部视图如下:
del IEnumerable<InterfaceMonterrey.Models.CheckBoxList>
<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>
然后在您的Controller Post方法中:
public class YourControllerNameController : Controller
{
[HttpPost]
public ActionResult Registrar(YourModel model, List<int> Value_Check)
{
// do the necessary staffs here with model and Value_Check
}
}
答案 1 :(得分:0)
如果要将表单中的值发布到控制器操作,请使用以下Html.BeginForm重载:
Html.BeginForm(string actionName, string controllerName, FormMethod method)
查看
@model ViewModelType
@using (Html.BeginForm("PostAction", "Home", FormMethod.Post)
{
//Rest of your form here
}
控制器
public class HomeController : Controller
{
public ActionResult PostAction(ViewModelType model)
{
//model should contain the values in the form
}
}
如果您要为发布操作命名,则可以使用方法重载:
public class HomeController : Controller
{
[HttpGet]
public ActionResult MiscPage()
{
var viewModel = new ViewModelType();
return View(viewModel);
}
[HttpPost]
public ActionResult MiscPage(ViewModelType model)
{
//model should contain the values in the form
//Save or whatever it is you want to do with the posted information
return View(model); //Or redirect somewhere else, up to you.
}
}
它可以在没有HttpGet / Post属性的情况下工作,但为清楚起见,我建议添加它们。