counerstrike
counerstrike

Reputation: 11

razor mvc5 c# web for input slider range

I'm trying to get a range slider input on my c# mvc 5 view page that i can submit the input to the mvc controller to write to a database. i tried using Jquery Ui Slider but i couldn't figure how to get the values over to my c# code for writing form data to database. here's the view cshtml file

@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")
}

and here's the Controller file

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);
        }
    }
}

Upvotes: 0

Views: 1660

Answers (1)

Shawnas
Shawnas

Reputation: 410

Most likely you have missed one of these:

a) does my element in view has corresponding name

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

b) have I included my wanted element in bindings list

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

these 2 are most likely reasons. And if still it is missing, check if you are actually posting all the data.

Upvotes: 1

Related Questions