2015-10-05 37 views
-1

我想獲得一個範圍滑塊輸入我的c#mvc 5查看頁面,我可以提交輸入到mvc控制器寫入數據庫。我試過使用Jquery Ui Slider,但我無法想象如何將值寫入我的c#代碼以將表單數據寫入數據庫。 這裏的景色CSHTML文件剃鬚刀mvc5 c#網絡輸入滑塊範圍

@model RateMyCourse.ReviewCourse 

@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 


@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>ReviewCourse</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Review, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Review, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Review, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Relevance, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Relevance, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Relevance, "", new { @class = "text-danger" }) 

      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Fun, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Fun, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Fun, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Difficulty, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Difficulty, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Difficulty, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Clarity, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Clarity, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Clarity, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Rating, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Rating, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Rating, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Like, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Like, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Like, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Dislike, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Dislike, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Dislike, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.CourseCourseId, "CourseCourseId", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("CourseCourseId", null, htmlAttributes: new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.CourseCourseId, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.User_UserId, "User_UserId", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("User_UserId", null, htmlAttributes: new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.User_UserId, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

和這裏的控制器文件

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.Entity; 
using System.Linq; 
using System.Net; 
using System.Web; 
using System.Web.Mvc; 
using RateMyCourse; 


namespace RateMyCourse.Controllers 
{ 
    public class ReviewCoursesController : Controller 
    { 
     private CollegeRocksEntities db = new CollegeRocksEntities(); 

     // GET: ReviewCourses 
     public ActionResult Index() 
     { 
      var reviewCourse = db.ReviewCourse.Include(r => r.Course).Include(r => r.User); 
      return View(reviewCourse.ToList()); 
     } 

     // GET: ReviewCourses/Details/5 
     public ActionResult Details(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      ReviewCourse reviewCourse = db.ReviewCourse.Find(id); 
      if (reviewCourse == null) 
      { 
       return HttpNotFound(); 

      } 
      return View(reviewCourse); 
     } 

     // GET: ReviewCourses/Create 
     public ActionResult Create() 
     { 
      ViewBag.CourseCourseId = new SelectList(db.Course, "CourseId", "Name"); 
      ViewBag.User_UserId = new SelectList(db.User, "UserId", "UserName"); 
      return View(); 
     } 

     // POST: ReviewCourses/Create 
     // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
     // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Create([Bind(Include = "ReviewCourseId,Review,Relevance,Fun,Difficulty,Clarity,Rating,Like,Dislike,CourseCourseId,User_UserId")] ReviewCourse reviewCourse) 
     { 
      if (ModelState.IsValid) 
      { 
       db.ReviewCourse.Add(reviewCourse); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      ViewBag.CourseCourseId = new SelectList(db.Course, "CourseId", "Name", reviewCourse.CourseCourseId); 
      ViewBag.User_UserId = new SelectList(db.User, "UserId", "UserName", reviewCourse.User_UserId); 
      return View(reviewCourse); 
     } 

     // GET: ReviewCourses/Edit/5 
     public ActionResult Edit(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      ReviewCourse reviewCourse = db.ReviewCourse.Find(id); 
      if (reviewCourse == null) 
      { 
       return HttpNotFound(); 
      } 
      ViewBag.CourseCourseId = new SelectList(db.Course, "CourseId", "Name", reviewCourse.CourseCourseId); 
      ViewBag.User_UserId = new SelectList(db.User, "UserId", "UserName", reviewCourse.User_UserId); 
      return View(reviewCourse); 
     } 

     // POST: ReviewCourses/Edit/5 
     // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
     // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Edit([Bind(Include = "ReviewCourseId,Review,Relevance,Fun,Difficulty,Clarity,Rating,Like,Dislike,CourseCourseId,User_UserId")] ReviewCourse reviewCourse) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Entry(reviewCourse).State = EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      ViewBag.CourseCourseId = new SelectList(db.Course, "CourseId", "Name", reviewCourse.CourseCourseId); 
      ViewBag.User_UserId = new SelectList(db.User, "UserId", "UserName", reviewCourse.User_UserId); 
      return View(reviewCourse); 
     } 

     // GET: ReviewCourses/Delete/5 
     public ActionResult Delete(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      ReviewCourse reviewCourse = db.ReviewCourse.Find(id); 
      if (reviewCourse == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(reviewCourse); 
     } 

     // POST: ReviewCourses/Delete/5 
     [HttpPost, ActionName("Delete")] 
     [ValidateAntiForgeryToken] 
     public ActionResult DeleteConfirmed(int id) 
     { 
      ReviewCourse reviewCourse = db.ReviewCourse.Find(id); 
      db.ReviewCourse.Remove(reviewCourse); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      if (disposing) 
      { 
       db.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 
    } 
} 
+1

你將哪個屬性應用於滑塊。滑塊的腳本在哪裏?刪除所有這些不相關的代碼,只顯示相關的內容。 –

回答

0

最有可能你已經錯過了其中的一個:

一)做我的視圖元素都有相應的

<input type="number" class="nmbr" name="second"/> 

b)是否已將我想要的元素包含在綁定列表中

public ActionResult Create([Bind(Include = "second")] ReviewCourse reviewCourse) 

這兩個是最有可能的原因。如果它仍然丟失,請檢查您是否實際發佈了所有數據。