Razor mvc5 c# web用于输入滑块范围

本文关键字:范围 输入 用于 mvc5 web Razor | 更新日期: 2023-09-27 18:16:04

我试图在我的c# mvc 5视图页面上获得一个范围滑块输入,我可以将输入提交给mvc控制器以写入数据库。我尝试使用Jquery Ui滑块,但我无法弄清楚如何将值转换为我的c#代码,用于编写表单数据到数据库。下面是视图CSHTML文件

@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")
}
这是Controller文件
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);
        }
    }
}

Razor mvc5 c# web用于输入滑块范围

很可能你错过了其中一个:

a)我的元素在视图中是否有相应的名称
<input type="number" class="nmbr" name="second"/>

b)我是否在绑定列表中包含了我想要的元素

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

这两个是最可能的原因。如果仍然没有,检查你是否真的发布了所有的数据。