如何在viewbag中传递where语句

时间:2015-09-30 10:56:04

标签: asp.net-mvc

我正在尝试将viewbag中的where语句传递给视图。我试图通过等于'In'的设备状态,但它不起作用。它不会显示条件。

ViewBag.EquipmentID = new SelectList(db.Equipments.Where(o => o .EquipmentStatus== "In"), "EquipmentID", "EquipmentType", hire.EquipmentID);

我选择设备的观点是:

<div class="form-group">
            @Html.LabelFor(model => model.EquipmentID, "EquipmentID", new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("EquipmentID", String.Empty)
                @Html.ValidationMessageFor(model => model.EquipmentID)
            </div>
        </div>

我的雇佣模式是:

  public class Hire
    {
        public int HireID { get; set; }
        public int EquipmentID { get; set; }
        public int PurchaseID { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime ReturnedDate { get; set; }

        public virtual Purchase Purchases { get; set; }
        public virtual Equipment Equipments { get; set; }
    }

使用viewmodel方法,它在视图中说“当前上下文中不存在名称'模型'” 这是我的观点:

 <div class="form-group">
            @Html.LabelFor(model => model.EquipmentID, "EquipmentID", new { @class = "control-label col-md-2" })
            <div class="col-md-10">
               @Html.DropDownListFor(model => model.EquipmentID, model.SelectList)
                @Html.ValidationMessageFor(model => model.EquipmentID)
            </div>
        </div>

'model.Equipment'中的'model'带有错误下划线

这是我的控制者:

   public ActionResult Create(int ID)
        {

           var hire = new Hire();
            Purchase purchase = db.Purchasess.Find(ID);
            ViewHire responseModel = new ViewHire()
            {
                PurchaseID = purchase.PurchaseID,
                EquipmentID = hire.EquipmentID,
                SelectList = new SelectList(db.Equipments.Where(o => o.EquipmentStatus == "In"), "EquipmentID", "EquipmentType", hire.EquipmentID),
            };



            ViewBag.PurchaseID = new SelectList(db.Purchasess, "PurchaseID", "PurchaseID");
            return View(responseModel );
        }

2 个答案:

答案 0 :(得分:1)

考虑一下:

ViewBag.Equipments = new SelectList(db.Equipments.Where(o => o .EquipmentStatus== "In"), "EquipmentID", "EquipmentType");

在你看来写:

@Html.DropDownListFor(model=>model.EquipmentID,(SelectList)ViewBag.Equipments,String.Empty)

答案 1 :(得分:1)

我认为问题在于您是通过ViewBag传递模型,而不是使用控制器中的View()将其传递给您的视图。

因此,当您使用像@Html.DropDownList(model => model.Property)这样的Html帮助程序时,它将找不到您想要传递的值。

您的视图应该在文件顶部定义一个视图模型,然后您的视图知道所期望的视图模型类型,例如:

@model MyViewModel

您的视图模型应定义为项目中的类,例如:

public class MyViewModel
{
    public int EquipmentId { get; set; }
    public SelectList SelectList { get; set; }
}

然后在您的控制器中,将值分配给您的类的实例:

public ActionResult Index(int id)
{
    // Code to retrieve hire and db instances...

    var responseModel = new MyViewModel()
    {
        EquipmentId = hire.EquipmentID,
        SelectList = new SelectList(db.Equipments.Where(o => o .EquipmentStatus== "In"), "EquipmentID", "EquipmentType", hire.EquipmentID),
    };

    // Return your view model to the view without using the ViewBag
    return View(responseModel);
}

现在您正在传递模型,您的HTML帮助程序将可以访问值:

@model MyViewModel
@Html.DropDownListFor(model => model.EquipmentId, model.SelectList)