In Model
1. Firstly create model class named Course and create property
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace StudentRecord.Models
{
public class Course
{
public int CourseId { get; set; }
[Display(Name = "Course Name")]
[Required(ErrorMessage = "Enter Course Name")]
public string CourseName { get; set; }
}
}
2. Then create another model class named as u wish. Here we use connection string and stored procedure.but i am using dll class here for connectivity.
public class DbConnectivity
{
private static string Config = System.Configuration.ConfigurationManager.ConnectionStrings["strCon"].ConnectionString;
public List<Course> DropCourse()
{
Database db = new SqlDatabase(Config);
DbCommand dbCommand = db.GetStoredProcCommand("DISPLAYCOURSE");
List<Course> list = new List<Course>();
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
{
while (dataReader.Read())
{
Course obj = new Course();
if (dataReader["Course"] != DBNull.Value)
{
if (dataReader["Course"] != DBNull.Value) { obj.CourseId = (int)dataReader["Course"]; }
if (dataReader["COURSENAME"] != DBNull.Value) { obj.CourseName = (string)dataReader["COURSENAME"]; }
list.Add(obj);
}
}
return list;
}
}
}
3.
In Controller
public ActionResult Drop()
{
DbConnectivity dbconnection = new DbConnectivity();
List<Course> pcontent = new List<Course>();
{
//Get all page content from TblHome table
pcontent = dbconnection.DisplayCourse();
};
List<SelectListItem> cityList = new List<SelectListItem>();
//List<string> items = new List<string>();
foreach (var item in pcontent)
{
cityList.Add(new SelectListItem
{
Text = item.CourseName,
Value = item.CourseId.ToString()
});
}
ViewBag.CityList = cityList;
return View();
//return View(pcontent);
}
In public ActionResult Drop()
{
DbConnectivity dbconnection = new DbConnectivity();
List<Course> pcontent = new List<Course>();
{
//Get all page content from TblHome table
pcontent = dbconnection.DisplayCourse();
};
List<SelectListItem> cityList = new List<SelectListItem>();
//List<string> items = new List<string>();
foreach (var item in pcontent)
{
cityList.Add(new SelectListItem
{
Text = item.CourseName,
Value = item.CourseId.ToString()
});
}
ViewBag.CityList = cityList;
return View();
//return View(pcontent);
}
4.
In View
@model StudentRecord.Models.Course
@{
ViewBag.Title = "Drop";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Drop</h2>
<fieldset>
<legend>Course</legend>
<div class="display-label">CourseName</div>
<div class="display-field">
@Html.DropDownList("CityName", (IEnumerable<SelectListItem>)ViewBag.cityList)
</div>
</fieldset>
@using (Html.BeginForm())
{
<p>
<input type="Submit" value="Submit" />
</p>
}
1. Firstly create model class named Course and create property
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace StudentRecord.Models
{
public class Course
{
public int CourseId { get; set; }
[Display(Name = "Course Name")]
[Required(ErrorMessage = "Enter Course Name")]
public string CourseName { get; set; }
}
}
2. Then create another model class named as u wish. Here we use connection string and stored procedure.but i am using dll class here for connectivity.
public class DbConnectivity
{
private static string Config = System.Configuration.ConfigurationManager.ConnectionStrings["strCon"].ConnectionString;
public List<Course> DropCourse()
{
Database db = new SqlDatabase(Config);
DbCommand dbCommand = db.GetStoredProcCommand("DISPLAYCOURSE");
List<Course> list = new List<Course>();
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
{
while (dataReader.Read())
{
Course obj = new Course();
if (dataReader["Course"] != DBNull.Value)
{
if (dataReader["Course"] != DBNull.Value) { obj.CourseId = (int)dataReader["Course"]; }
if (dataReader["COURSENAME"] != DBNull.Value) { obj.CourseName = (string)dataReader["COURSENAME"]; }
list.Add(obj);
}
}
return list;
}
}
}
3.
In Controller
public ActionResult Drop()
{
DbConnectivity dbconnection = new DbConnectivity();
List<Course> pcontent = new List<Course>();
{
//Get all page content from TblHome table
pcontent = dbconnection.DisplayCourse();
};
List<SelectListItem> cityList = new List<SelectListItem>();
//List<string> items = new List<string>();
foreach (var item in pcontent)
{
cityList.Add(new SelectListItem
{
Text = item.CourseName,
Value = item.CourseId.ToString()
});
}
ViewBag.CityList = cityList;
return View();
//return View(pcontent);
}
In public ActionResult Drop()
{
DbConnectivity dbconnection = new DbConnectivity();
List<Course> pcontent = new List<Course>();
{
//Get all page content from TblHome table
pcontent = dbconnection.DisplayCourse();
};
List<SelectListItem> cityList = new List<SelectListItem>();
//List<string> items = new List<string>();
foreach (var item in pcontent)
{
cityList.Add(new SelectListItem
{
Text = item.CourseName,
Value = item.CourseId.ToString()
});
}
ViewBag.CityList = cityList;
return View();
//return View(pcontent);
}
4.
In View
@model StudentRecord.Models.Course
@{
ViewBag.Title = "Drop";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Drop</h2>
<fieldset>
<legend>Course</legend>
<div class="display-label">CourseName</div>
<div class="display-field">
@Html.DropDownList("CityName", (IEnumerable<SelectListItem>)ViewBag.cityList)
</div>
</fieldset>
@using (Html.BeginForm())
{
<p>
<input type="Submit" value="Submit" />
</p>
}
Leave Comment