Urgent: Post data back to controller in List<SomeClass>

3 posts, 1 answers
  1. Ravinder
    Ravinder avatar
    5 posts
    Member since:
    Mar 2014

    Posted 07 Apr 2014 Link to this post

    I posted the same my be in some wrong category. Please answer ASAP as I am in critical Situation.

    Hi,
    I am new to Kendo UI widgets and MVC4. I am using Razor View. I have a model Called Project. I have another model called Location. In Project model there is a property "List<LocationType> Locations". 

    On my razor form i have "@model Model.Project" and one of my multiselect widget shows me location list. I have declared it "@(Html.Kendo().MultiSelectFor(model => model.LocanTypes)". Now when I try to get "Project.Locations" in controller in [HttpPost] action I get Project.Locations as null.

    Model declaration on Razor Page: 
    @model PestPec.Models.Project 

    Multiselect declartion of Multiselect:
    @(Html.Kendo().MultiSelectFor(model => model.LocanTypes)
                            .Name("LocationTypesMultiSelect")
                            .Placeholder("Select Location Type(s)")
                            .DataTextField("Name")
                            .DataValueField("ID")
                            .Filter(FilterType.Contains)
                            .DataSource(source =>
                            {
                                source.Read(read =>
                                {
                                    read.Action("GetLocatinTypes", "Project");
                                });
                            }) 
                        ) 

    Project.cs:
     public class Project
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public int Type { get; set; }
            public string CreatedBy { get; set; }
            public List<Technician> Technicians{get; set;}
            public List<Route> Routes { get; set; }
            public List<ProjService> Services { get; set; }
            public List<Division> Divisions { get; set; }
            public List<Branch> Branches { get; set; }
            public List<LocationType> LocanTypes { get; set; }
            public String Country { get; set; }
            public String City { get; set; }

            [DisplayFormat(DataFormatString = "{0:MM/dd/yy}")]
            public DateTime CreatedDate { get; set; }

            [DisplayFormat(DataFormatString = "{0:MM/dd/yy}")]
            public DateTime LastUpdatedDate { get; set; }
        }

    LocationType:
    public class LocationType
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }
  2. Ravinder
    Ravinder avatar
    5 posts
    Member since:
    Mar 2014

    Posted 07 Apr 2014 in reply to Ravinder Link to this post

    I changed my MultiSelect widget to: 

    @(Html.Kendo().MultiSelectFor(model => model.Technicians)
                                .Name("Technicians")
                                .Placeholder("Select Technician(s)")
                                .DataTextField("Name")
                                .DataValueField("ID")
                                .Filter(FilterType.Contains)
                                .AutoBind(true)
                                .DataSource(source =>
                                {
                                    source.Read(read =>
                                    {
                                        read.Action("GetTechnicians", "Project");
                                    })
                                    .ServerFiltering(false);
                                })
                                .BindTo(Model.Technicians)          
                            )

    Now I am getting "Technicians" object with 0 length instead of null.
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Answer
    Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 09 Apr 2014 Link to this post

    Hello,

    The multiselect renders a select element and therefore only the values and not the entire items will be posted to the server when using a form. You should add a collection parameter of the value field type  to the action method with the same name as the property name in order to get the values on the server. 
    public ActionResult Action(Project project, List<int> technicians)


    Regards,
    Daniel
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top