You must Sign In to post a response.
  • Category: ASP.Net MVC

    Bind drop down with preselected value

    Hello,

    I have an issue with binding drop down with selected value.I am using list to generate dropdown with selected value on cshtml page or view. When my first loop is complete, my dropdown is bind properly, but i just want to select ID of loop in bind drop down.I tried like :-

    <tbody>
    @for (int i = 0; i < Model.LeaveClass.Count; i++)
    {
    <tr><td>
    @Html.DropDownListFor(m => m.LeaveClass[i].ID, new selectList(Model.LeaveTypes, "Text", "Value"), "Select")
    </td></tr>
    }
    </tbody>
  • #768512
    You can set the SelectedValue by directly binding the SelectedID in the markup like:
    SelectedValue='<%# Bind("SelectedID") %>'

    Code snippet for directly binding the SelectedID
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if(e.Row.RowType== DataControlRowType.DataRow)
    {
    DataRowView drv = e.Row.DataItem as DataRowView;
    DropDownList ddlCategories = e.Row.FindControl("DdlCategories") as DropDownList;
    if(ddlCategories != null)
    {
    ddlCategories.SelectedValue = drv["SelectedID"].ToString();
    }
    }

    Useful reference: http://www.telerik.com/forums/bind-selectedvalue-after-populating-dropdownlist


  • Sign In to post your comments