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

本文深入探讨了控制器中使用[HttpPost]修饰符的Edit方法,详细介绍了如何通过模型状态验证、数据库操作以及视图展示实现评论的编辑过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>  

 

        

转载于:https://www.cnblogs.com/stonetogreece/archive/2012/06/27/2566486.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值