【问题标题】:How to pass data from partial view to main controller - ASP.NET MVC如何将数据从局部视图传递到主控制器 - ASP.NET MVC
【发布时间】: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


【解决方案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
    }
}

【讨论】:

    【解决方案2】:

    如果要将值从表单发布到控制器操作,请使用以下 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
        }
    }
    

    如果您想将您的 post 操作命名为与您的 get 相同,您可以使用方法重载:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-18
      • 1970-01-01
      • 2014-06-11
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 2013-10-10
      相关资源
      最近更新 更多