Quantcast
Viewing latest article 3
Browse Latest Browse All 4

Dropdownlist,checkbox,radio button Validation

Hi,

I need to validate the dropdownlist,radio button,checkbox

Here is the code

Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCTest.Controllers
{
    public class MainController : 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);
            ViewData["list"] = list;

           
            return View();
            
        }
}
}

Model:
using System.Data.Entity;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;

namespace MVCTest.Models
{
    public class MainModel
    {
        [Required]

        [Display(Name = "User name")]
        public string lblUserName { get; set; }

        public string txtUserName {get;set;}
        public string lblName {get;set;}

        [Required]

        [Display(Name = "Latest thinking")]
        public string lblLatestThinking {get;set;}
        public string ddlLatestThinking {get;set;}

        

        [Display(Name = "Role")]

        public string lblRole {get;set;}

        [Required(ErrorMessage= "Please enter Role")]
        public string txtRole {get;set;}



    }
}

View:
@model MVCTest.Models.MainModel

@{
    ViewBag.Title = "Create";
}<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset><legend>MainModel</legend><div class="editor-label">
            @Html.LabelFor(model => model.lblUserName)</div><div class="editor-field">
              @Html.LabelFor(model => model.txtUserName)
            @Html.ValidationMessageFor(model => model.txtUserName)</div><div class="editor-label">
            @Html.LabelFor(model => model.lblLatestThinking)</div><div class="editor-field">
            @Html.DropDownList("list", ViewData["list"] as SelectList,"--Select--")
            @Html.ValidationMessage("list")</div><div class="editor-label">
            @Html.LabelFor(model => model.lblRole)</div><div class="editor-field">
          @Html.TextBoxFor(model => model.txtRole)
            @Html.ValidationMessageFor(model => model.txtRole)</div><p><input type="submit" value="Create"  name="command"/> &nbsp<input type="submit" value="Cancel"  name="command"/></p></fieldset>
}<div>
    @Html.ActionLink("Back to List", "Index")</div>

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

Here dropdownlist is not validated . need help.

Also please provide code to validaate checkboxlist,radio button in MVC .

sAM


Viewing latest article 3
Browse Latest Browse All 4

Trending Articles