简体   繁体   English

我在另一个viewmodel中有一个viewmodel列表。 如何将参数从动作传递到控制器?

[英]I have a list of viewmodel in another viewmodel. How can I pass parameter from action to controller?

I have a ViewModel containing a list of another ViewModel. 我有一个ViewModel包含另一个ViewModel的列表。 I want to pass parameters to my controller. 我想将参数传递给控制器​​。 I can pass all viewmodel parameters but my list of second viewmodel is null. 我可以传递所有viewmodel参数,但第二个viewmodel的列表为null。

This is my viewmodel: 这是我的视图模型:

public class Provider_Coupon_FullViewModel {
    public int pid { get; set; }
    public int cid { get; set; }
    public int crid { get; set; }
    public string opr_user { get; set; }
    public List<PackageDetailsViewModel> PackageDetails { get; set; }
}

public class PackageDetailsViewModel {
    public int packages_id { get; set; }
    public string package_title { get; set; }
}

And this is my razor page: 这是我的剃须刀页面:

@model IList<myproject.Provider_Coupon_FullViewModel>
@using (Html.BeginForm())
{
    for (var i = 0; i < Model.Count; i++)
    {
    <div class="form-horizontal col-lg-12 col-md-12 col-sm-12 col-xs-12">           
        @Html.EditorFor(Model => Model[i].pid)
        @Html.EditorFor(Model => Model[i].cid)
        @Html.EditorFor(Model => Model[i].crid)

        /// I want show checkbox for list of viewmodel and pass value of them if check box checked
        @for (int j = 0; j < Model[i].PackageDetails.Count; j++)
        {
            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 FormBox">
                <input type="checkbox" id=@Model[i].PackageDetails[j].packages_id name=@Model[i].PackageDetails[j].package_title value=@Model[i].PackageDetails[j].packages_id class="check">
                <label for="@Model[i].PackageDetails[j].packages_id">@Model[i].PackageDetails[j].package_title</label>
            </div> 
        }
        @for (int j = 0; j < Model[i].PackageDetails.Count; j++)
        {
            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 FormBox">
                <input type="checkbox" id=@Model[i].PackageDetails[j].packages_id name=@Model[i].PackageDetails[j].package_title value=@Model[i].PackageDetails[j].packages_id class="check">
                <label for="@Model[i].PackageDetails[j].packages_id">@Model[i].PackageDetails[j].package_title</label>
            </div>
        }
    </div>
    }
}

Instead of rendering the checkboxes yourself, you should let MVC handle that by using Html.CheckBoxFor(m => ...) . 与其自己渲染复选框, Html.CheckBoxFor(m => ...)让MVC使用Html.CheckBoxFor(m => ...)来处理它。 MVC will generate form element names that exactly match what it needs when the posted data is being parsed, which is very hard to do for yourself. MVC将生成与解析发布的数据时所需的表单元素名称完全匹配的表单元素名称,这对您自己来说很难做到。

Also, to render a checkbox this way you need to have a bool property in your ViewModel, but that is missing. 另外,要以这种方式呈现复选框,您需要在ViewModel中具有bool属性,但这是缺少的。

What follows is working code (actually tested). 接下来是工作代码(经过实际测试)。

(Before we continue, one more essential thing: don't use foreach as the comment above states. Reason: MVC CheckBoxFor() only generates proper form element names (that can be parsed by MVC again) if you use for + [indexes] at all levels of the ViewModel, whereas that ability would fall apart if you'd use foreach at any level. MVC needs and uses those index numbers.) (在继续之前,还有一件非常重要的事情:不要使用foreach作为上面的注释。原因:如果您将MVC CheckBoxFor() for + [indexes],它只会生成正确的表单元素名称(可以再次由MVC解析)。在ViewModel的所有级别上使用,但如果您在任何级别上使用foreach ,该功能都会崩溃。MVC需要并使用这些索引号。)

Controller: 控制器:

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace Test_MVC.Controllers
{
    public class TestController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            // Create some data for initial display
            var packageModels1 = new[] {
                new PackageDetailsViewModel { packages_id = 101, package_title = "Order 101", is_selected = false },
                new PackageDetailsViewModel { packages_id = 102, package_title = "Order 102", is_selected = true },
                new PackageDetailsViewModel { packages_id = 103, package_title = "Order 103", is_selected = false }
            };
            var packageModels2 = new[] {
                new PackageDetailsViewModel { packages_id = 201, package_title = "Order 201", is_selected = false },
                new PackageDetailsViewModel { packages_id = 202, package_title = "Order 202", is_selected = false }
            };
            var couponModels = new[] {
                new Provider_Coupon_FullViewModel { cid = 1, crid = 2, opr_user = "John", pid = 4, PackageDetails = packageModels1.ToList() },
                new Provider_Coupon_FullViewModel { cid = 7, crid = 8, opr_user = "Beth", pid = 6, PackageDetails = packageModels2.ToList() }
            };

            return View(couponModels.ToList());
        }

        [HttpPost]
        public ActionResult Index(List<Provider_Coupon_FullViewModel> model)
        {
            // Pass whatever was posted & parsed right back into the view for verification
            return View(model);
        }
    }
}

ViewModel classes: ViewModel类:

public class Provider_Coupon_FullViewModel
{
    public int pid { get; set; }
    public int cid { get; set; }
    public int crid { get; set; }
    public string opr_user { get; set; }
    public List<PackageDetailsViewModel> PackageDetails { get; set; }
}

public class PackageDetailsViewModel
{
    public int packages_id { get; set; }
    public string package_title { get; set; }
    public bool is_selected { get; set; } /////// <--- BOOL PROPERTY <--- ///////
}

Razor View: 剃刀视图:

@model IList<Provider_Coupon_FullViewModel>
@using (Html.BeginForm())
{
    for (var i = 0; i < Model.Count; i++)
    {
        <div style="border: dashed 1px blue; margin: 5px; padding: 5px">
            pid: @Html.EditorFor(Model => Model[i].pid)<br />
            cid: @Html.EditorFor(Model => Model[i].cid)<br />
            crid: @Html.EditorFor(Model => Model[i].crid)<br />

            @*I want show checkbox for list of viewmodel and pass value of them if check box checked*@
            @for (int j = 0; j < Model[i].PackageDetails.Count; j++)
            {
                <div class="">
                    i = @i / j = @j / ID: @Html.DisplayFor(m => Model[i].PackageDetails[j].packages_id) / Title: @Html.DisplayFor(m => Model[i].PackageDetails[j].package_title) /
                    @Html.CheckBoxFor(m => Model[i].PackageDetails[j].is_selected)
                    @Html.LabelFor(m => Model[i].PackageDetails[j].is_selected)
                </div>
            }
        </div>
    }
    <input type="submit" value="Submit" />
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从控制器将ViewModel传递给Webapi方法 - How can I pass a viewmodel to a webapi method from a controller 如何在我的ViewModel中侦听来自另一个ViewModel的更改? - How can I listen in my ViewModel to changes from another ViewModel? 复杂的ViewModel。 视图返回一个空的ViewModel。 [HttpPost]动作 - Complex ViewModel. View returns an empty ViewModel. [HttpPost] Action 从ViewModel编辑和保存。 - Editing and saving from a ViewModel. 如何将视图模型数据从视图传递到控制器? - How can I pass my viewmodel data from my view to my controller? 发布MVC表单,调用需要视图模型的控制器操作视图。 为什么未将ViewModel发送到视图? - Post MVC form calling a controller action view that requires a viewmodel. Why does the ViewModel not get sent to the view? 如何让一个viewmodel更新另一个viewmodel上的属性? - How can I make one viewmodel update a property on another viewmodel? 如何将参数从parentViewModel传递到ViewModel? - How can I pass parameters from a parentViewModel to my ViewModel? 从View直接调用ViewModel。 可以自行更改DataContext吗? - Direct calls from View to ViewModel. Can DataContext be changed on itself? Windows Phone 7-无法从ViewModel触发事件。 - Windows Phone 7 - Can not trigger the event from ViewModel.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM