当用户从下拉列表中选择一个项目时,必须在mvc 4中为该用户删除该项目

时间:2015-07-24 08:55:53

标签: html5 asp.net-mvc-4 razor

我正在创建一个用于学习MVC 4的用户课程项目。在这个项目中,当用户登录时,他们可以看到他们的课程和其他细节。用户可以创建或编辑课程。所以我在创建和编辑期间在下拉列表中显示课程列表。我想要的是,当用户选择课程并将其添加到他的课程列表中时,该课程不应显示在该用户的下拉列表中。下面给出了一些创建代码:

CourseController:

public ActionResult Create()
{
    User user = (User)Session["User"];
    var usr = db.Users.Find(user.UserId);
    if (Session["User"] != null)
    {             
        ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "CourseName");
        ViewBag.UserId = usr.UserId;
        ViewBag.FirstName = usr.FirstName;
    }
    return View();
}
//
// POST: /Course/Create

[HttpPost]
public ActionResult Create(UserCourse usercourse)
{
    User user = (User)Session["User"];
    var usr = db.Users.Find(user.UserId);
    if (Session["User"] != null)
    {
        if (ModelState.IsValid)
        {
            db.UserCourses.Add(usercourse);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "CourseName", usercourse.CourseId);
        ViewBag.UserId = usr.UserId;//from c in db.Users.Where(u => u.UserId == usercourse.UserId) select c;
        ViewBag.FirstName = usr.FirstName;
        return View();
    }
    return View(usercourse);
}

Create.cshtml:

<div class="editor-label">
    @Html.DisplayNameFor(model => model.CourseId)
</div>
<div class="editor-field">
    @Html.DropDownList("CourseId", String.Empty)
    @Html.ValidationMessageFor(model => model.CourseId)
</div>
<p>
    <input type="submit" value="Create" />
</p>

用户的模型类:

public partial class User
{
    public User()
    {
        this.UserCourses = new HashSet<UserCourse>();
    }
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [Required(ErrorMessage = "Please Provide User Name", AllowEmptyStrings = false)]
    public string UserName { get; set; }
    [Required(ErrorMessage = "Please Provide Password", AllowEmptyStrings = false)]
    [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
    public string Password { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Email { get; set; }
    public string PhoneNo { get; set; }

    public virtual ICollection<UserCourse> UserCourses { get; set; }
}

课程的模型类:

public partial class Course
{
    public Course()
    {
        this.UserCourses = new HashSet<UserCourse>();
    }

    public int CourseId { get; set; }
    public string CourseName { get; set; }

    public virtual ICollection<UserCourse> UserCourses { get; set; }
}

UserCourse的模型类:

public partial class UserCourse
{
    public int Id { get; set; }
    public Nullable<int> UserId { get; set; }
    public Nullable<int> CourseId { get; set; }

    public virtual Course Course { get; set; }
    public virtual User User { get; set; }
}

请回复。

1 个答案:

答案 0 :(得分:1)

[已编辑]:这样试试:

  public ActionResult Create()
{
    User user = (User)Session["User"];
    var usr = db.Users.Find(user.UserId);
    if (Session["User"] != null)
    {   
        var courses = db.Courses.Where(x=>!x.UserCourses.Any(y=>y.CourseId == x.CourseId && y.UserId == usr.UserId)).ToList();

        ViewBag.CourseId = new SelectList(courses , "CourseId", "CourseName");
        ViewBag.UserId = usr.UserId;
        ViewBag.FirstName = usr.FirstName;
    }
    return View();
}
//
// POST: /Course/Create

[HttpPost]
public ActionResult Create(UserCourse usercourse)
{
    User user = (User)Session["User"];
    var usr = db.Users.Find(user.UserId);
    if (Session["User"] != null)
    {
        if (ModelState.IsValid)
        {
            db.UserCourses.Add(usercourse);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        var courses = db.Courses.Where(x=>!x.UserCourses.Any(y=>y.CourseId == x.CourseId && y.UserId == usr.UserId)).ToList();

        ViewBag.CourseId = new SelectList(courses, "CourseId", "CourseName", usercourse.CourseId);
        ViewBag.UserId = usr.UserId;//from c in db.Users.Where(u => u.UserId == usercourse.UserId) select c;
        ViewBag.FirstName = usr.FirstName;
        return View();
    }
    return View(usercourse);
}