你的位置:首页 > ASP.net教程

[ASP.net教程]MVC同一页面循环显示数据库记录(答题/投票系统)


//控制器代码
public ActionResult vote(int id=1) { //int id = 1; list newlist = db.lists.Find(id); //var q = from p in db.lists where p.id==1 select p; //list newlist = new list { id=q.id}; //var tes = q; return View(newlist); } [HttpPost] public ActionResult vote(list newlist) { //var q = Request.Form["item.id"]; //var p = Convert.ToInt32(Response.Cookies["id"].Value); al newal = new al { uid = Convert.ToInt32(Request.Cookies["id"].Value), lid = newlist.id, result = Request.Form["result"] }; db.als.Add(newal); db.SaveChanges(); int a = newlist.id + 1; list next = db.lists.Find(a); return RedirectToAction("vote", new { id = a }); }
试图代码
@model votesys.list@{  ViewBag.Title = "vote";}<h2>vote</h2>@using (Html.BeginForm()) {  @Html.AntiForgeryToken()  @Html.ValidationSummary(true)  <fieldset>    <legend>list</legend>    @Html.HiddenFor(model => model.id)    <div class="editor-label">      @Html.LabelFor(model => model.question)    </div>    <div class="editor-field">      @Html.EditorFor(model => model.question)      @Html.ValidationMessageFor(model => model.question)    </div>    <div class="editor-label">      @Html.LabelFor(model => model.remark)    </div>    <div class="editor-field">      @Html.EditorFor(model => model.remark)      @Html.ValidationMessageFor(model => model.remark)    </div>    <div>      <input type="radio" id="GenderM" name="result" value="公司制度很完善" />选项一      <input type="radio" id="GenderF" name="result" value="人员分配合理" checked />选项二    </div>    <p>      <input type="submit" value="Save" />    </p>  </fieldset>}<div>  @Html.ActionLink("Back to List", "Index")</div>@section Scripts {  @Scripts.Render("~/bundles/jqueryval")}