Validate DropDownList using Client Side and Server Side Validation in ASP.Net MVC

Last Reply on Oct 06, 2014 01:06 AM By saini1987tarun

Posted on Oct 03, 2014 04:27 AM

Hi I am new to Asp.Net MVC 

How to create a page like this and Validation using both on server side and Client side.

And how The dropdown can be retrieved from DB and Similarly how to get all the radio buttons values from the DB.

Afteer Entering the details how to store it in the DB.
Kindly help on it...


Posted on Oct 03, 2014 04:40 AM

Please refer this code to bind the DropDownList in MVC.

View

Index.aspx

This is my first MVC application
<br />
List of Employees name
<%: @Html.DropDownList("ddlEmployees", (IEnumerable<SelectListItem>)ViewData["Employees"])%>

Namespaces

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

Controller

public class Default1Controller : Controller
{
    public ActionResult Index()
    {
        ViewData["Employees"] = GetEmployees();
        return View();
    }
    public List<SelectListItem> lst = new List<SelectListItem>();
        
    public IEnumerable<SelectListItem> GetEmployees()
    {
        string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT EmployeeId, FirstName FROM Employees", con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    lst.Add(new SelectListItem() { Text = "--Select--", Value = "" });
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        lst.Add(new SelectListItem() { Text = dr["FirstName"].ToString(), Value = dr["EmployeeId"].ToString() });
                    }
                }
            }
        }

        return lst;
    }
}

Keep you View inside shared folder of View.

Screenshot


I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html