【问题标题】:How to pass an array from main view to partial view in asp.net mvc 5如何在asp.net mvc 5中将数组从主视图传递到部分视图
【发布时间】:2017-04-23 21:30:14
【问题描述】:

我有一个主视图,其中包含从相应 Action 接收的数组,它还包含下面的部分视图参考

创建.cshtml

@model HrAndPayrollSystem.Models.EmployeeMasterA

@using (Html.BeginForm())
{
    ViewData["fs_lbls"] = ViewBag.FS_lbls as string[];
    @Html.Partial("~/Views/EmployeeMasterA/FinalSettlementTAB.cshtml", Model)
}

上面引用的局部视图定义如下

FinalSettlementTAB.cshtml

@model HrAndPayrollSystem.Models.EmployeeMasterA

    @Html.DropDownList("DeptId", null, "Department")   

/* Print "ViewData["fs_lbls"]" array defined in the Main View `Create.cshtml` here */

我在Create.cshtml 中定义了一个数组,现在,我想将它传递到部分视图HR_EmployeeFinalSettlementTAB.cshtml 并打印它,正确的方法是什么?

我尝试过的:

我将@Html.Partial() 行更改为以下内容:

 @Html.Partial("~/Views/EmployeeMasterA/FinalSettlementTAB.cshtml", null, new ViewDataDictionary { { "fs_lbls", ViewData["fs_lbls"] } })

并修改 FinalSettlementTAB.cshtml 文件如下:

@model HrAndPayrollSystem.Models.EmployeeMasterA

@Html.DropDownList("DeptId", null, "Department")   

@foreach (var i in ViewData["fs_lbls"] as string[])
{
    @i
}

但它会在@Html.DropDownList("DeptId", null, "Department") 行抛出异常InvalidOperationException 说:

没有具有键“DeptId”的“IEnumerable”类型的 ViewData 项。

每当我尝试使用ViewDataDictionary 将数组数据传递给局部视图时,它都会引发上述异常,否则,它工作正常,而我不是。

如何摆脱上述异常并正确地将数组数据从主视图传递到部分视图?

【问题讨论】:

    标签: razor asp.net-mvc-5 asp.net-mvc-partialview


    【解决方案1】:

    我建议你在EmployeeMasterA添加一个新的属性来存储标签,这样你就完全不需要使用ViewData了。

    public class EmployeeMasterA
    {
        public string[] fs_lbls { get; set; }
    
        public string SelectedLabel { get; set; }
    
        public List<SelectListItem> Labels
        {
            get
            {
    
                if (this.fs_lbls == null)
                {
                    return Enumerable.Empty<SelectListItem>().ToList();
                }
    
                return (from label in fs_lbls
                       select new SelectListItem
                {
                    Text = label,
                    Value = label
                }).ToList();
            }
        }
    }
    

    创建.cshtml

    @model WebApplication1.Controllers.EmployeeMasterA
    
    @using (Html.BeginForm())
    {
        @Html.Partial("FinalSettlementTAB", Model)
        <input type="submit" value="Save"/>
    }
    

    FinalSettlementTAB.cshtml

    @model WebApplication1.Controllers.EmployeeMasterA
    
    @Html.DropDownList("SelectedLabel", Model.Labels)
    

    控制器

    public ActionResult Create()
    {
        var viewModel = new EmployeeMasterA();
        viewModel.fs_lbls = new[] {"Label1", "label 2"};
    
        return View(viewModel);
    }
    
    [HttpPost]
    public ActionResult Create(EmployeeMasterA viewModel)
    {
        return View();
    }
    

    您可以在控制器操作方法中设置fs_lbls 的内容,然后返回Create 视图。当您发布表单时,SelectedLabel 属性将包含从下拉列表中选择的项目。显然,您需要更改属性名称以满足您的需要,但希望这会给您一个想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      • 2019-06-23
      • 1970-01-01
      • 2016-12-15
      • 2016-06-26
      • 1970-01-01
      • 2012-12-07
      相关资源
      最近更新 更多