I want to insert class_ID column Dropdownlist selected value into database, but value is inserted 0.
public class SaveDataController : Controller
{
// GET: SaveData
public ActionResult Index()
{
tbl_Student student =new tbl_Student();
student.ClassList = GetClasss();
ViewBag.saveresult ="";
return View(student);
}
[HttpPost]
public ActionResult Index(tbl_Student student)
{
using (SchoolEntities2 entities =new SchoolEntities2())
{
student.std_id = student.std_id;
student.std_Name = student.std_Name;
student.class_ID = student.class_ID;
entities.tbl_Student.Add(student);
entities.SaveChanges();
ViewBag.SaveResult ="Data Save";
ModelState.Clear();
}
}
private static List<SelectListItem> GetClasss()
{
SchoolEntities2 entities =new SchoolEntities2();
List<SelectListItem> classList = (from pin entities.tbl_Class
selectnew SelectListItem
{
Text = p.Class_Name,
//Value = p.Class_ID
Value = SqlFunctions.StringConvert((double)p.Class_ID)
}).ToList();
//Add Default Item at First Position.
classList.Insert(0,new SelectListItem { Text ="--Select Class--", Value ="" });
return classList;
}
}
}
<div>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table border="1" bgcolor="yellow" width="400px" height="250px">
<tr>
<td>Roll Number</td>
<td> @Html.TextBoxFor(m => m.std_id, "{0:#.#}")</td>
</tr>
<tr>
<td>Student Name</td>
<td> @Html.TextBoxFor(m => m.std_Name)</td>
</tr>
<tr>
<td>Class</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit" />
</td>
</tr>
<tr></tr>
</table>
}
</div>