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

Populating Dropdown Lists When Editing

1 Answer 117 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 30 Aug 2016, 07:39 PM

I'm trying to figure out how to populate a dropdown list when editing a ListView item. I'm binding to a ViewModel that contains an IEnumerable<SelectListItem>. On other pages without a ListView, I do something like below, however I'm not sure what action I can use to set up the model so that I have access to it when editing using the ListView.  I tried looping through each item in the Read action and setting States there, which is definitely inefficient, but regardless didn't work.  Any ideas?

public IActionResult Index()
{
    var model = _service.FindById(1));
    model.States = (_lookupService.GetStates());
    return View(model);
}

<select asp-for="StateId" asp-items="Model.States" class="form-control">
    <option value=""></option>
</select>

1 Answer, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 01 Sep 2016, 10:57 AM
Hello Michael,

Have you created editor template that to be used for editing? If this is the case you can save the data into the ViewData and then use it for biding.

EditorTemplate:
@(Html.Kendo().DropDownListFor(p=> p.States)
                    .BindTo(ViewData["states"]))

Controller
public ActionResult Index()
{
    ViewData["states"] = _lookupService.GetStates();
    return View();
}

Please also check these forums, where you can find more information and working sample:
http://www.telerik.com/forums/listview-editing-with-dropdownlist
http://www.telerik.com/forums/ddl-binding-in-edit-templete-of-listview

Regards,
Vasil
Telerik by Progress
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
ListView
Asked by
Michael
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Share this question
or