【问题标题】:MVC How to use IEnumerable variables stored in ViewBag in the View?MVC 如何在视图中使用存储在 ViewBag 中的 IEnumerable 变量?
【发布时间】:2015-05-22 20:31:47
【问题描述】:

下面是View中出现错误的简化代码:

型号:

    public class Employee
    {
        public string EmployeeID{ get; set; }
        public string Name { get; set; }
        ...
    }

控制器:

    public ActionResult Index()
    {
        var model = selectAllEmployees();
        ViewBag.ITDept = model.Where(a => a.departmentID == 4);
        ViewBag.Officer = model.Where(a => a.departmentID == 5);
        return View(model);
    }

查看:

@model IList<EnrolSys.Models.Employee>

@{
    Layout = null;
}

@using (Html.BeginForm("Save", "EmployMaster"))
{
    for (int i = 0; i < ViewBag.ITDept.Count(); i++)
    {
        //Here's the error occurs
        @Html.Partial("EmployeeDisplayControl", ViewBag.ITDept[i])
    }
    <br />
}

@Html.Partial("EmployeeDisplayControl", ViewBag.ITDept[i])这行有一个异常:

'System.Web.Mvc.HtmlHelper>' 没有名为“Partial”的适用方法,但似乎有一个 该名称的扩展方法。扩展方法不能动态 派出。考虑强制转换动态参数或调用 没有扩展方法语法的扩展方法。

我猜是说我不能在动态表达式中使用扩展方法,有什么解决方法吗??

我为这个错误做了一个小提琴: https://dotnetfiddle.net/ekDH06

【问题讨论】:

  • 试试System.Web.Mvc.HtmlHelper.Partial(@Html, "EmployeeDisplayControl", ViewBag.ITDept[i]);
  • 或者干脆@Html.Partial("EmployeeDisplayControl", (object)ViewBag.ITDept[i])
  • 在这种情况下您对@Html.Partial() 的使用将不起作用(顺便说一句,您链接到我的答案),因为部分不会生成正确的名称属性
  • 您需要从包含两个属性List&lt;Employee&gt; ITDeptEmployeesList&lt;Employee&gt; OfficerEmployees 的视图模型开始,然后在每个集合上使用for 循环(使用@Html.TextBoxFor(m =&gt; m[i].Name) 或使用自定义EditorTemplate
  • @User2012384,Alex Art 的回答也是正确的,所以你应该接受那个。

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

当你使用

ViewBag.ITDept = model.Where(a => a.departmentID == 4);

您在Viewbag.ITDept 中获得IEnumerable,而不是IList。这意味着您不能使用索引器(例如 ViewBag.ITDept[i]),因为 IEnumerable 不支持随机访问。

一个解决方案:

ViewBag.ITDept = model.Where(a => a.departmentID == 4).ToList();

现在它一个列表,所以你可以使用索引器。

其他解决方案:不要使用“for”循环,而是使用“foreach”:

foreach (var employee in ViewBag.ITDept)
{
    @Html.Partial("EmployeeDisplayControl", employee )
}

也许您仍然需要将该 ViewBag.ITDept 转换为 IEnumerable&lt;Employee&gt;

【讨论】:

    【解决方案2】:

    您可以为此使用编辑器/显示模板:

    public class YourViewModel
    {
       public IList<Employee> ITDept {get; set;}
       public IList<Employee> Officers {get; set;}
       //other properties here
    }
    

    为您的 Employee 模型定义一个编辑器或显示模板(您应该将其放置在 Views/Shared/EditorTemplates 或 Views/Shared/DisplayTemplates 下):

    模板可能是这样的(当然是简化版):

    @model EnrolSys.Models.Employee
    
    <div>
       @Html.EditorFor(m=>m.Name)
    </div>
    

    现在 Index 操作的视图将接收 YourViewModel 作为模型 你可以简单地使用:

    @model YourViewModel 
    
    
    @using (Html.BeginForm("Save", "EmployMaster"))
    {
        <div>
            @Html.EditorFor(m=>m.ITDept)
        </div>
    }
    

    【讨论】:

      【解决方案3】:

      您需要为动态表达式提供静态类型。试试这个:

      @Html.Partial("EmployeeDisplayControl", (object)ViewBag.ITDept[i])
      

      【讨论】:

        猜你喜欢
        • 2019-11-18
        • 1970-01-01
        • 1970-01-01
        • 2016-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-19
        相关资源
        最近更新 更多