Quantcast
Channel: Dropdownlist,checkbox,radio button Validation
Viewing all articles
Browse latest Browse all 4

Re: Dropdownlist,checkbox,radio button Validation

$
0
0

It should be working.

Are you trying to perform client-side validation (i.e. prior to the form being posted)? If so - you will need to make sure to include the necessary client-side validation libraries :

<!-- jQuery and other validation libraries --><script src="~/Scripts/jquery-1.10.2.min.js"></script><script src="~/Scripts/jquery.validate.min.js"></script><script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

I've throw together a very basic example seen below that should work :

ExampleController.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    public class ExampleController : Controller
    {
        public ActionResult Create(string command)
        {

            var list = new SelectList(new[] {
                                              new {ID="1",Name="name1"},
                                              new{ID="2",Name="name2"},
                                              new{ID="3",Name="name3"},
                                          },"ID", "Name", 1);
            ViewBag.List = list;
            return View();
        }

        [HttpPost]
        public ActionResult Created(ExampleModel model)
        {
            // Check if the model is valid
            if (!ModelState.IsValid)
            {
                // Rebuild the View data
                var list = new SelectList(new[] {
                                              new {ID="1",Name="name1"},
                                              new {ID="2",Name="name2"},
                                              new {ID="3",Name="name3"},
                                          },"ID", "Name", 1);
                ViewBag.List = list;

                // It in invalid, return the previous view
                return View("Create");
            }
            return View();
        }
    }

    public class ExampleModel
    {
        [Required]
        public string List { get; set; }
    }

}

along with the following View (with an option added to the DropDown to have no default selection) :

@model WebApplication1.Controllers.ExampleModel<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width" /><title>Create</title></head><body><script src="~/Scripts/jquery-1.10.2.min.js"></script><script src="~/Scripts/jquery.validate.min.js"></script><script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script><form action="@Url.Action("Created","Example")" method='post'>
        @Html.AntiForgeryToken()<div class="form-horizontal"><h4>ExampleModel</h4><hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })<div class="form-group">
                @Html.LabelFor(model => model.List, htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10">
                    @Html.DropDownList("List", (ViewBag.List as SelectList),"--Select--", new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.List, "", 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></form></body></html>

 


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images