如何将数据从局部视图传递到主控制器 - ASP.NET MVC

问题描述 投票:0回答:2

我是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
2个回答
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
    }
}

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属性的情况下工作,但为了清晰起见,我建议添加它们。

© www.soinside.com 2019 - 2024. All rights reserved.