I want to save dropdownlist selected value in database
My models code as follows
public partial class Leave1
{
public int LeaveID { get; set; }
public string LeaveType { get; set; }
public string FromDate { get; set; }
public string ToDate { get; set; }
public string Reason { get; set; }
}
My controller code as follows
[HttpGet]
public ActionResult AddorEdit1()
{
Leave1 Leave1Model = new Leave1();
return View(Leave1Model);
}
[HttpPost]
public ActionResult AddorEdit1(Leave1 Leave1Model)
{
using (SampleEntities3 dbModel = new SampleEntities3())
{
dbModel.Leave1.Add(Leave1Model);
dbModel.SaveChanges();
}
ModelState.Clear();
ViewBag.successmessage = "Leave Request Submitted Successfully";
return View("Addoredit1", new Leave1());
}
My view code as follows
@model UserRegistration.Models.Leave1
@{
ViewBag.Title = "Leave";
}
Leave
@using (Html.BeginForm("AddorEdit1","Leave",FormMethod.Post)) {
@Html.ValidationSummary(true)
Leave
@Html.LabelFor(model => model.LeaveType)
@Html.DropDownList("ddlleave", new List<selectlistitem>
{ new SelectListItem { Text = "Casual Leave", Value = "1", Selected=true},
new SelectListItem { Text = "Sick Leave", Value = "2"}
} , "Select LeaveType")
}
But in run mode when i run the above code dropdownlist selected value is not saved in the database
In database it shows as
LeaveType
Null
it database it saves the null value.
In my above code controller or view code what is the mistake.
What I have tried:
I want to save dropdownlist selected value in database
My models code as follows
public partial class Leave1
{
public int LeaveID { get; set; }
public string LeaveType { get; set; }
public string FromDate { get; set; }
public string ToDate { get; set; }
public string Reason { get; set; }
}
My controller code as follows
[HttpGet]
public ActionResult AddorEdit1()
{
Leave1 Leave1Model = new Leave1();
return View(Leave1Model);
}
[HttpPost]
public ActionResult AddorEdit1(Leave1 Leave1Model)
{
using (SampleEntities3 dbModel = new SampleEntities3())
{
dbModel.Leave1.Add(Leave1Model);
dbModel.SaveChanges();
}
ModelState.Clear();
ViewBag.successmessage = "Leave Request Submitted Successfully";
return View("Addoredit1", new Leave1());
}
My view code as follows
@model UserRegistration.Models.Leave1
@{
ViewBag.Title = "Leave";
}
<h2>Leave</h2>
@using (Html.BeginForm("AddorEdit1","Leave",FormMethod.Post)) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Leave</legend>
<div class="editor-label">
@Html.LabelFor(model => model.LeaveType)
</div>
<div class="editor-field">
@Html.DropDownList("ddlleave", new List<SelectListItem>
{ new SelectListItem { Text = "Casual Leave", Value = "1", Selected=true},
new SelectListItem { Text = "Sick Leave", Value = "2"}
} , "Select LeaveType")
</div>
</fieldset>
}
But in run mode when i run the above code dropdownlist selected value is not saved in the database
In database it shows as
LeaveType
Null
it database it saves the null value.
In my above code controller or view code what is the mistake.