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

Issues With Displaying Selected Value InDropDownList

3 Answers 58 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Marcab
Top achievements
Rank 1
Veteran
Marcab asked on 26 Dec 2019, 06:39 PM

I'm having quite a bit of difficulty displaying selected/default values in a DropDownList. I created an appropriate ViewModel:

namespace KendoVolunteerApp.Models.ViewModels
{
    public class OrganizationViewModel
    {
        public int OrganizationId { get; set; }
        public string Name { get; set; }
    }
}

and Controller

using KendoVolunteerApp.Models;
using KendoVolunteerApp.Models.ViewModels;
using System.Linq;
using System.Web.Mvc;
namespace KendoVolunteerApp.Controllers
{
    public class DropDownListController : Controller
    {
        private VolunteersDbContext db = new VolunteersDbContext();
        // GET: DropDownList
       
        public ActionResult DropDownList()
        {
            return View();
        }
        public JsonResult DropDownList_GetOrganizations()
        {
            var organizations = db.Organizations.Select(organization => new OrganizationViewModel
            {
                OrganizationId = organization.OrganizationId,
                Name = organization.Name
            });
            //if  (!string.IsNullOrEmpty(text))
            //{
            //    organizations = organizations.Where(p => p.Name.Contains(text));
            //}
            return Json(organizations, JsonRequestBehavior.AllowGet);
        }
    }
}

The DropDownList is part of a custom editor template.

. . .

 <div>
            <div class="form-group col-md-4">
                <div class="k-label-top">
                    @Html.LabelFor(model => model.Organization)
                </div>
                <div class="k-dropdown">
                    @(Html.Kendo().DropDownListFor(model => model.Organization)
                           .Name("organizations")
                           .Filter("contains")
                           .DataTextField("Name")
                           .DataValueField("OrganizationId")
                           .OptionLabel("Select organization....")
                           .DataSource(source =>
                           {
                               source.Read(read =>
                               {
                                   read.Action("DropDownList_GetOrganizations", "DropDownList");
                               }).ServerFiltering(true);
                              
                           })
                           .HtmlAttributes(new { style = "width: 300px" })                    )
                </div>
            </div>

. . .

 

The DropDownList is being populated, as it should. My issue is that I need to have a particular organization selected by default, in those scenarios where a volunteer is part of an organization. Organization is not  required field, as not all volunteers belong to an organization. There is no relationship between the two tables. The name of the organization is stored in the organization field of the volunteer table.

It's setting this selected/default value that is giving me problems.

I've been through various examples, but could not find anything that met my needs. Any help or suggestions would be very much appreciated.

 

Thank You

 

3 Answers, 1 is accepted

Sort by
0
Veselin Tsvetanov
Telerik team
answered on 30 Dec 2019, 10:17 AM

Hi Marcab,

Attached you will find a small sample, based on the snippets sent. If I correctly understood the scenario in question, you would need to set a default value for the DropDownList under some conditions. To do so, I would suggest you use the controller action that would return the view containing the form (and the DropDownList) like below:

var model = new VolunteerViewModel();

// Here you could access the organization field that is stored for the volonteer in question
// For the needs of the example we will assume the value of that field is "second"
var org = GetAllOrgs().Find(o => o.Name == "second");
model.Organization = org.OrganizationId;

return View(model);

Note that the sample attached does not include the Kendo.Mvc.dll, which you will need to reference.

Regards,
Veselin Tsvetanov
Progress Telerik

Get quickly onboarded and successful with your Telerik UI for ASP.NET MVC with the dedicated Virtual Classroom technical training, available to all active customers.
0
Marcab
Top achievements
Rank 1
Veteran
answered on 02 Jan 2020, 01:15 PM

Thank you Veselin. I'll give your solution a try.

The condition is that the selected value for the DropDownList needs to match the Organization value stored in the Volunteer table. The Volunteer table stores the name of the organization, rather than the Organization ID, as there is no relationship between the two tables (not every Volunteer belongs to an Organization).

0
Veselin Tsvetanov
Telerik team
answered on 06 Jan 2020, 11:24 AM

Hi Marcab,

Give my suggestion a try and let me know if you have any further questions. As per selecting by organization name, with my suggestion above you will query the Organizations table and find the item with the name present in the current model (Volunteer). This way you could get the OrganizationId and pass it to the DropDownList to select the appropriate value.

Regards,
Veselin Tsvetanov
Progress Telerik

Get quickly onboarded and successful with your Telerik UI for ASP.NET MVC with the dedicated Virtual Classroom technical training, available to all active customers.
Tags
DropDownList
Asked by
Marcab
Top achievements
Rank 1
Veteran
Answers by
Veselin Tsvetanov
Telerik team
Marcab
Top achievements
Rank 1
Veteran
Share this question
or