0

如果产品已分配给客户,我如何从下拉列表中删除它?

Id 的顺序似乎正确。我做了一些改变,也许我错过了什么

        public ActionResult Edit(int id = 0)
    {     
        UserProfile userprofile = db.UserProfiles.Find(id); 

        if (userprofile == null)
        {
            return HttpNotFound();
        }

        var deviceList = db.Devices.ToList();
        var userList = db.UserProfiles.ToList();

        foreach (var user in userList)
        {
            deviceList.RemoveAll(x=>x.DeviceID==user.Device_DeviceID);          

        }

            ViewBag.deviceList = new SelectList(db.Devices, "DeviceID", "DeviceIMEI", userprofile.Device_DeviceID);              
            return View(userprofile);

    }

    // POST: /User/Edit/5
    [HttpPost]
    public ActionResult Edit(UserProfile userprofile)
    {                     
        if (ModelState.IsValid)
        {
           db.Entry(userprofile).State = EntityState.Modified;

            //db.UserProfiles.Attach(userprofile);

            db.SaveChanges();

            return RedirectToAction("Index");
        }
        ViewBag.deviceList= new SelectList(db.Devices, "DeviceID", "DeviceIMEI", userprofile.Device_DeviceID);            
        return View(userprofile);
    }
4

1 回答 1

0

试试RemoveAll方法。

 var deviceList = db.Devices.ToList();
 var userList = db.UserProfiles.ToList();
 foreach (var user in userList)
 {
    deviceList.RemoveAll(x=>x.DeviceID==user.Device_DevideID);
 }

现在您可以绑定devicelist到视图的下拉列表。我强烈建议避免使用 ViewBag/ViewData 方法并切换到强类型视图方法。

于 2013-06-12T12:15:08.510 回答