Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
Suvendu Shekhar Giri 21-Feb-18 4:50am    
Please share only the relevant code.
[no name] 21-Feb-18 5:03am    
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());
}

Above i sent the code for saving dropdownlist value into database

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900