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

[Solved] Basic DropDowlList Troubles

7 Answers 111 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Michael
Top achievements
Rank 1
Michael asked on 09 Mar 2011, 12:47 PM
I really can't get my head arounbd this dropdownlist functionality in an edit page in MVC

My code successfully shows the extenbsive list of "directors" but I cannot see how to have the correct (current) director selected as the form shows??

Im confused about the ajax binding and EVERYTHING?

any help or a reference to an example would be appreciated

heres my code as it stands

 

 

 

 

 

 

 

<div class="editor-label">
    @Html.LabelFor(model => model.Race1.DirectorId)
</div>
<div class="editor-field">
    @(Html.Telerik().DropDownListFor(model=>model.Race1.DirectorId)
    .Name("DirectorId")
    .BindTo(new SelectList(Model.Directors, "Value", "DisplayText"))
    )
    @Html.ValidationMessageFor(model => model.Race1.DirectorId)
</div>

 

7 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 09 Mar 2011, 01:19 PM
Hi Michael,

You can check the server edit templates example which shows how to add a dropdownlist in the grid editor template. 

Regards,
Atanas Korchev
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Michael
Top achievements
Rank 1
answered on 10 Mar 2011, 05:41 AM
Thanks for the heads up on this, however two things:

The question posted in this forum was for a combo box/dropdownlist in a simple edit page Is this the same thing??

Also not linked to an actual Employee (in your example, from the EditableOrder.Employee) but rather I only have the EmployeeID in my model

the EmployeeID of which would be selected from a combobox full of the possible Employees in my edit form

Am i missing the point?

Can I still use "UIHint("Employee")  even if my field is only EmployeeID??

Also: if using Razor do i still have these templates saved as ascx files??
0
Georgi Krustev
Telerik team
answered on 10 Mar 2011, 04:30 PM
Hello Michael,

I have created a simple test project which shows how to use ComboBox UI component in an editor template. Please note the Index.cshtml (Razor) and the Customer.ascx (WebForms). The Editor template which is UserControl can be easily used in a Razor page.

Kind regards,
Georgi Krustev
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Michael
Top achievements
Rank 1
answered on 12 Mar 2011, 08:10 AM
Appreciate the sample but have a few questions:

theres an actionresult that returns JSON data called GetCustomers.... I cant see that used anywhere?

Also what are the requirements for "auto matching" the fields in the editorfor?

For instance, can I have a "BasicListItem" class that just contains "ID" and "name" and fill these with customers/Products 

would the "magic" still work or will MVC go looking for "CustomerID" and ProductsID??

ALSO

you use .SelectedIndex in the template??  wouldnt this break the autoselecting of the correct member of the dropdownlist?
0
Georgi Krustev
Telerik team
answered on 14 Mar 2011, 12:49 PM
Hello Michael,

 
GetCustomers Action method was not used in the previously attached test project. It can be used to bind ComboBox UI component in Ajax mode. I have attached the modified test project which shows how to do that.

"auto matching" functionality in the CombBox/DropDownList UI component works like in the Html.DropDownList extension. If ViewData.Eval("Component name") returns value it will be used to select value from the List<SelectListItem>. Depending on the given implementation in the test project, you can select item with JavaScript. I have also add Html.DropDownListFor extension which shows that the correct item is not selected in this case.

Component should be named exactly the same way as the key in the ViewData in order to get "auto matching " functionality.

SelectedIndex will not select different item, because value get from ViewData has bigger priority.

Kind regards,
Georgi Krustev
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Michael
Top achievements
Rank 1
answered on 15 Mar 2011, 02:59 PM
OK there must be something else wrong with my code

this works & shows the selected value as per the model's data
<div class="editor-field">
    @(Html.Telerik().ComboBoxFor(model => model.Race1.LocationID)
    .BindTo(Model.Locations as SelectList))
    @Html.ValidationMessageFor(model => model.Race1.LocationID)
</div>

but the changes dont appear in the data back on the controller

similarly for other fields.

the start of this edit view declares
@model WAC_MVC.ViewModels.RaceEditViewModel
@{

and the post controller action is

[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
    Race race = db.GetRaceById(id);
 
    if (TryUpdateModel(race))
    {
        db.SaveChanges();
 
        return RedirectToAction("Index", new { id = race.RACEID });
    }
 
    RaceEditViewModel returnVM = new RaceEditViewModel(race);
 
    return View(returnVM);
}

im really not understanding the samples youve sent im sorry
0
Michael
Top achievements
Rank 1
answered on 16 Mar 2011, 12:06 AM
Found this on Stackoverflow

http://stackoverflow.com/questions/1242758/what-is-correct-behaviour-of-updatemodel-in-asp-net-mvc

Seems I had to use the viewmodel as my "tryUpdate" target and all was good

this is contrary to the nerdinner example offered by msdn

so my code is all as above with the alteration to the controller so that:

[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
    Race Race1 = db.GetRaceById(id);
    RaceEditViewModel returnVM = new RaceEditViewModel(Race1);
 
    if (TryUpdateModel(returnVM))
    {
        db.SaveChanges();
 
        return RedirectToAction("Index", new { id = Race1.RACEID });
    }
     
    return View(returnVM);
}
 
Tags
ComboBox
Asked by
Michael
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Michael
Top achievements
Rank 1
Georgi Krustev
Telerik team
Share this question
or