MVC中如何选取后台数据展示下拉列表项

Controller中的Edit方法


        [HttpPost] 
  public ActionResult Edit(Comment comment)
        {
            if (ModelState.IsValid)
            {
                db.Entry(comment).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UserId = new SelectList(db.Users, "UserId""Email", comment.UserId);
            ViewBag.PostId = new SelectList(db.Posts, "PostId""Title", comment.PostId);
            return View(comment);
        }

 

 View中展示:

<div class="editor-label">
            @Html.LabelFor(model => model.UserId, "User")
        </div>
        <div class="editor-field">
            @Html.DropDownList("UserId", String.Empty)
            @Html.ValidationMessageFor(model => model.UserId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.PostId, "Post")
        </div>
        <div class="editor-field">
            @Html.DropDownList("PostId", String.Empty)
            @Html.ValidationMessageFor(model => model.PostId)
</div>  

 

        

posted @ 2012-06-27 23:02  月小  阅读(873)  评论(0)    收藏  举报