【问题标题】:How to access the child lists in cshtml page through model如何通过模型访问cshtml页面中的子列表
【发布时间】:2019-01-15 14:58:15
【问题描述】:

无法访问 cshtml 页面中的模型列表类型变量。 这是我的模型类

public class MBilling
{
    public List<string> plist { get; set; }
    public List<decimal> qlist { get; set; }
    public List<decimal> splist { get; set; }
}

控制器

public ActionResult Billing()
{
    if (Session["UserId"] != null)
    {
        MBilling binddata = BillingService.getBindData();
        return View(binddata);
    }
    else
    {
        return this.RedirectToAction("Index", "Login");
    }
}

这里是cshtml部分

@model IEnumerable<Shop.Models.MBilling>
@{
    ViewBag.Title = "Billing";
}

<input style="border:none" list="browsers" name="browser">

<datalist id="browsers">
    ///Unable to ACCESS  Model.plist
    @foreach (var item in Model.plist)
    {
    }
</datalist>

我能够从服务文件中获取具有所需数据的模型对象,但在 cshtml 页面中显示错误

CS1061:“IEnumerable&lt;MBilling&gt;”不包含“plist”的定义,并且找不到接受“IEnumerable&lt;MBilling&gt;”类型的第一个参数的扩展方法“plist”(您是否缺少 using 指令或程序集参考?)

提前致谢。

【问题讨论】:

  • 你的 rmodel 是一个 IEnumerable,所以你需要一个索引。 Model[i].plist 或许?

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


【解决方案1】:

问题:

您将模型作为MBilling 类型的对象返回,但在视图中将其用作IEnumerable&lt;MBilling&gt;

解决办法:

这里不需要IEnumerable&lt;T&gt;,因为它不是可枚举的。所以删除它,然后做:

@model Shop.Models.MBilling

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 2020-09-02
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    相关资源
    最近更新 更多