Asp.net mvc 如何将数据从局部视图传递到主控制器-ASP.NET MVC
我是ASP.NETMVC新手。我做了一个局部视图,动态显示一个复选框。我的问题是如何获取复选框的值并将值发送到主控制器 当我提交表单的值时,我不知道如何将值从局部视图传递给控制器 这是我的控制器:Asp.net mvc 如何将数据从局部视图传递到主控制器-ASP.NET MVC,asp.net-mvc,checkbox,asp.net-mvc-partialview,Asp.net Mvc,Checkbox,Asp.net Mvc Partialview,我是ASP.NETMVC新手。我做了一个局部视图,动态显示一个复选框。我的问题是如何获取复选框的值并将值发送到主控制器 当我提交表单的值时,我不知道如何将值从局部视图传递给控制器 这是我的控制器: [ChildActionOnly] public ActionResult ListaCheckVistaParcial() { return PartialView("ListaCheckVistaParcial", ListaCheckBox()); } 以下是主要观点: @model
[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>
}
@model interface interrey.Models.UserAccount\u RolAcceso
@{
ViewBag.Title=“注册者”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
登记员
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
乌萨里奥
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.Usuario_InterfaceMty.USERID,htmlAttributes:new{@class=“control label col-md-2”})
@EditorFor(model=>model.Usuario_InterfaceMty.USERID,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Usuario_InterfaceMty.USERID,“,new{@class=“text danger”})
@LabelFor(model=>model.Usuario_InterfaceMty.NOMBRE,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Usuario_InterfaceMty.NOMBRE,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Usuario_InterfaceMty.NOMBRE,“,new{@class=“text danger”})
@LabelFor(model=>model.Usuario_interface.APELLIDO,htmlAttributes:new{@class=“control label col-md-2”})
@EditorFor(model=>model.Usuario_InterfaceMty.APELLIDO,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Usuario_InterfaceMty.APELLIDO,“,new{@class=“text danger”})
@LabelFor(model=>model.Usuario_InterfaceMty.USEREMAIL,htmlAttributes:new{@class=“control label col-md-2”})
@EditorFor(model=>model.Usuario_InterfaceMty.USEREMAIL,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Usuario_InterfaceMty.USEREMAIL,“,new{@class=“text danger”})
@LabelFor(model=>model.Usuario_interface.PASSWORD,htmlAttributes:new{@class=“control label col-md-2”})
@EditorFor(model=>model.Usuario_InterfaceMty.PASSWORD,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Usuario_InterfaceMty.PASSWORD,“,new{@class=“text danger”})
@*
@LabelFor(model=>model.LOGIN_TIMESTAMP,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.LOGIN_TIMESTAMP,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.LOGIN_TIMESTAMP,“,new{@class=“text danger”})
*@
@*@LabelFor(model=>model.PERFILID,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.PERFILID,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.PERFILID,“,new{@class=“text danger”})
*@
@Html.Label(“PERFIL”)
@Html.DropDownListFor(m=>m.Usuario_interface.PERFILID,新选择列表(ViewBag.perfilacecoso,“ID”,“description”),“SELECCIONE UN PERFIL”,新{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Usuario_InterfaceMty.PERFILID,“,new{@class=“text danger”})
@*
@LabelFor(model=>model.FECHAREGISTRO,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.FECHAREGISTRO,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.FECHAREGISTRO,“,new{@class=“text danger”})
*@
@*
@LabelFor(model=>model.ACTIVO,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.ACTIVO,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.ACTIVO,“,new{@class=“text danger”})
*@
@LabelFor(model=>model.Usuario_InterfaceMty.ACTIVO,htmlAttributes:new{@class=“control label col-md-2”})
@CheckBoxFor(m=>m.Usuario_interface.ActivoBool,htmlAttributes:new{@class=“null”})
@Html.ValidationMessageFor(model=>model.Usuario_InterfaceMty.ACTIVO,“,new{@class=“text danger”})
@*阿西格纳罗尔
@foreach(列表ViewBag.ListaCB中的变量项)
{
@Label(item.Nombre_ChB,htmlAttributes:new{@class=“control Label col-md-2”})
@CheckBoxFor(x=>item.Value\u检查,htmlAttributes:new{@class=“null”})
}
*@
@Html.Action(“ListaCheckVistaParcial”)
}
以下是我的部分观点:
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>
}
del IEnumerable
@使用(Html.BeginForm())
{
@*@Html.AntiForgeryToken()*@
阿西格纳罗尔
@foreach(模型中的var项目)
{
@Label(item.Nombre_ChB,htmlAttributes:new{@class=“control Label col-md-2”})
@CheckBoxFor(x=>item.Value\u检查,htmlAttributes:new{@class=“null”})
}
}
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.
}
}
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>
public class YourControllerNameController : Controller
{
[HttpPost]
public ActionResult Registrar(YourModel model, List<int> Value_Check)
{
// do the necessary staffs here with model and Value_Check
}
}