-1

How to get the selected drown drop value from database? I'm storing employee details in one table with drop down selected value(integer) and in other table drop down value and text.

my models are

public class EmployeeDetails
{
    public int empid {set;get;}
    public string empname{set;get;}
    public string empdesig{set;get;}
    public decimal empsal{set;get;}
    public int education{set;get;}
    public DateTime time{set;get;}
    public string dropdownname { set; get; }
}

public class DropDownList
{
    public int dropdownId { set; get; }
    public string dropdownName { set; get; }
}

public class DisplayEmployeeViewModel
{
    public IEnumerable<EmployeeDetails> EmployeeDetails { set; get; }
    public DisplayCreateEmployeeViewModel createemployee { set; get; }
    public string TabName { set; get; }
    public List<SelectListItem> DropdownSelectListItems
    {
        get
        {
            var dropdown = (new DropdownRepository()).GetDropdownTypes();
            var entityList = dropdown.Select(x => new SelectListItem()
            {
                Text = x.dropdownName,
                Value = x.dropdownId.ToString()
            });
            return entityList.ToList();
        }
    }
}

public class DisplayCreateEmployeeViewModel
{
    public EmployeeDetails EmployeeDetails { set; get; }
    public int empid { set; get; }
    public string empname { set; get; }
    public string empdesig { set; get; }
    public decimal empsal { set; get; }
    public int education { set; get; }
    public DateTime time { set; get; }
    public string dropdownname { set; get; }
    public List<SelectListItem> DropdownSelectListItems
    {
        get
        {
            var dropdown = (new DropdownRepository()).GetDropdownTypes();
            var entityList = dropdown.Select(x => new SelectListItem()
            {
                Text = x.dropdownName,
                Value = x.dropdownId.ToString()
            });
            return entityList.ToList();
        }
    }
}

View

<div>
    <table id="tblemployeedetails" class="table table-hover table-bordered table-responsive "  >
        <thead style="background-color:black;color:white; ">
            <tr>
                <th></th>
                <th>Employee Name</th>
                <th>Employee Designation</th>
                <th>Enployee Education</th>
                <th>Employee Salary</th>
            </tr>
        </thead>
        <tbody>
            @foreach (EmployeeDetails details in Model.EmployeeDetails)
            {
                <tr>
                    <td>@Html.CheckBox("status" + details.empid, new {value=details.empid,@class="detailCheckBox" })</td>
                    <td>@Html.TextBox(details.empname, details.empname, new {@class="txtdisable" })</td>
                    <td>@Html.TextBox(details.empdesig, details.empdesig, new { @class = "txtdisable" })</td>
                    <td>@Html.DropDownList(details.education.ToString(),Model.DropdownSelectListItems, new { @class = "txtdisable" })</td>
                    <td>@Html.TextBox(details.empsal.ToString(), details.empsal, new { @class = "txtdisable" })</td>
                </tr>
            }
        </tbody>        
    </table>
</div>

The code which I'm showing fetching the drop down complete list and showing. Here I'm getting the list of drop down. But I want to get the list with selected text( stored value should select)

how to do that

rammi
  • 81
  • 1
  • 12
  • First refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) - you cannot use a `foreach` loop to bind to a collection. Then once you have corrected that, refer [this answer](http://stackoverflow.com/questions/37407811/mvc5-razor-html-dropdownlistfor-set-selected-when-value-is-in-array/37411482#37411482) –  Jun 17 '16 at 03:25

1 Answers1

0

When you're selecting into SelectListItem, just add:

Selected = x.dropdownName == dropdownname

However, that really shouldn't be required, as long as there's a value in ModelState the select list can bind to. I'm not sure exactly what you're doing in your view, but the first parameter to Html.DropDownList should correspond to a property on your model, as it would need to in order to bind back to that property on post. If it does match up to a property, then Razor will use the value of that property to automatically set the appropriate selected value in the select list. For what it's worth, this is much easier when using the *For family of helpers:

@Html.DropDownListFor(m => m.SomeProperty, Model.SomePropertyChoices)

That way, you know by the expression that you're binding to something real, instead of just hoping your string name is correct and will bind.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444