下拉菜单不加载到所选项目上

时间:2019-11-07 09:39:11

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

我无法使用模型上所选选项的下拉菜单加载编辑用户页面。添加用户时,它已正确保存在数据库中。

我尝试将其绑定到编辑视图中,但由于某种原因,它无法正常工作。

User.cs(模型)

公共字符串ActivityLevel {get;组; }

控制器

    List<SelectListItem> activity = new List<SelectListItem>
    {
        new SelectListItem{ Selected = false ,Text = "Sedentary (little or no exercise)", Value = "Sedentary (little or no exercise)" },
        new SelectListItem{ Selected = false ,Text = "Lightly active (light exercise/sports 1-3 days/week)", Value = "Lightly active (light exercise/sports 1-3 days/week)"  },
        new SelectListItem{ Selected = false ,Text = "Moderately active (moderate exercise/sports 3-5 days/week)", Value = "Moderately active (moderate exercise/sports 3-5 days/week)"  },
        new SelectListItem{ Selected = false ,Text = "Very active (hard exercise/sports 6-7 days a week)", Value = "Very active (hard exercise/sports 6-7 days a week)"  },
        new SelectListItem{ Selected = false ,Text = "Extra active (very hard exercise/sports & a physical job)", Value = "Extra active (very hard exercise/sports & a physical job)"  },
    };

公共ActionResult NewUser()         {

        User u = new User();
        ViewBag.User = u;
        ViewBag.activity = activity;
        return View();
    }
    [HttpPost]
    public ActionResult NewUser(User user)
    {
        try
        {
            db.Users.Add(user);
            db.SaveChanges();
            return RedirectToAction("AllUsers");
        }
        catch (Exception e)
        {
            return View();
        }
    }



    public ActionResult EditUser(int id)
    {
        User user = db.Users.Find(id);
        ViewBag.User = user;


        activity.Single(i => i.Value == user.ActivityLevel).Selected = true;
        ViewBag.activity = activity;

        return View();
    }
    [HttpPut]
    public ActionResult EditUser(int id, User requestUser)
    {
        try
        {
            User user = db.Users.Find(id);
            if (TryUpdateModel(user))
            {
                user.Firstname = requestUser.Firstname;
                user.Lastname = requestUser.Lastname;
                user.Height = requestUser.Height;
                user.Weight = requestUser.Weight;
                user.Gender = requestUser.Gender;
                user.Age = requestUser.Age;
                user.Calories = requestUser.Calories;
                user.Bmr = requestUser.Bmr;
                user.ActivityLevel = requestUser.ActivityLevel;
                user.WeightChoice = requestUser.WeightChoice;
                db.SaveChanges();
            }
            return RedirectToAction("AllUsers");
        }
        catch (Exception e)
        {
            return View();
        }
    }

Edituser(视图):(我认为这是问题所在,或者是edit方法的get控制器)

<label>Select Activity level</label>

@Html.DropDownListFor(m => m.ActivityLevel,
new SelectList(ViewBag.activity, "Value", "Text"),
new { @class = "form-control" })

新用户(查看):

<label>Select Activity level</label>

@Html.DropDownListFor(m => m.ActivityLevel,
new SelectList(ViewBag.activity, "Value", "Text"),
new { @class = "form-control" })

1 个答案:

答案 0 :(得分:0)

请检查以下解决方案: 请分开您的代码,将您选择的值传递到其他Viewbag中,例如Viewbag.SelectedID和 通过您在ViewBag.activity EditUser中传递的所有内容 在Edituser用户界面

中这样使用
@Html.DropDownListFor(m => m.ActivityLevel, new SelectList(ViewBag.activity, "Value", "Text", Viewbag.SelectedID), new { @class = "form-control" })