This is a migrated thread and some comments may be shown as answers.

Default Item in Dropdown for Custom List

1 Answer 150 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Emily
Top achievements
Rank 1
Emily asked on 23 Sep 2015, 02:45 PM

I have a Target field in my PopupEditor that's using custom List to display a dropdown in the View. I want the first item in the custom list (Self) to show as the default value in the dropdown.

View:

@Html.LabelFor(model => model.Target, new { @class = "col-sm-5 control-label" })
<div>
    @Html.EditorFor(model => model.Target)
</div>

Controller:

public ActionResult Index()
{
    ViewData["Targets"] = ixList.Targets();
 
    return View();
}

Custom List (ixList):

public static List<TargetsModel> Targets()
{
    List<TargetsModel> targets = new List<TargetsModel>();
 
    targets.Add(new TargetsModel { Description = "Self", Target = "_self" });
    targets.Add(new TargetsModel { Description = "Blank", Target = "_blank" });
 
    return targets;
}

Model:

[UIHint("GridForeignKey")]
public string Target { get; set; }  // target = _blank, _self

 

How would I go about setting the first item Self to the default value? Right now the default value appears to be null.

 

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Vladimir Iliev
Telerik team
answered on 28 Sep 2015, 07:38 AM
Hi Emily,

You achieve the desired behavior by setting the default value directly in the Grid DataSource by passing the first item from the ViewData array of "Targets":


.DataSource(dataSource => dataSource
    .Ajax()     
    .ServerOperation(false)
    .Model(model => {
        model.Id(p => p.ID);
        model.Field(p => p.ID).Editable(false);
        model.Field(p => p.Target).DefaultValue(((List<TargetsModel>)ViewData["Targets"])[0].Target);

Regards,
Vladimir Iliev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Editor
Asked by
Emily
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Share this question
or