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

how can I get the correct CategoryID selected from dropdownlist and save it ?

5 Answers 158 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Wassim
Top achievements
Rank 1
Veteran
Iron
Wassim asked on 10 Nov 2020, 05:55 PM

this is my modal body .. it's a form to create a new product that should write a name and associate it to a catogory

 

                @model mySolution.Models.
                @using (Html.BeginForm("Product_Add", "", FormMethod.Post))
                {
                    @Html.AntiForgeryToken()
                    <div class="form-horizontal" id="newProduct">

                        <div id="validation-summary">
                        </div>
                        <div class="form-group">
                            <div class="form-row">
                                @Html.LabelFor(model => model.ProductName, htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.ProductName, "", new { @class = "text-danger" })
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="form-row">
                                @Html.LabelFor(model => model.ProductCategory, htmlAttributes: new { @class = "control-label col-md-2" })

                                @(Html.Kendo().DropDownList()
                                      .Name("categoriesDropDownList")
                                      .DataTextField("CategoryName")
                                      .DataValueField("CategoryID")

                                      .DataSource(source =>
                                      {
                                        source.Custom()
                                        .ServerFiltering(true)
                                        .Type("aspnetmvc-ajax")       

                                       .Transport(transport =>

                                        {
                                            transport.Read("Category_Read", "Category");
                                        })
                                        .Schema(schema =>
                                        {
                                            schema.Data("Data")
                                            .Total("Total");
                                        });
})
                                        )
                            </div>
                        </div>

                    </div>
                    <br />
                    <div class="form-group">
                        <div class="form-row">
                            <div class="col-md-12" style="text-align: right;">
                                <input type="submit" value="Enregister" class="mr-1 mb-1 btn btn-primary" />
                                @Html.ActionLink("Annuler", "ProductList", "Products", null, new { @class = "mr-1 mb-1 btn btn-secondary" })
                               
                            </div>
                        </div>
                    </div>

                }

 

Product and Category are two differents table..I get the list of categories but when i want to submit the save of a new product ..i didn't get the correct value of categoryID and after submit :in the table of Product..it take the correct Name of Product filled and 0 as value of CategoryID 

in the controller side i have an action method Product_Add(ProductModel model) that call a service to execute the save..

 

how can I get the correct CategoryID selected , pass it (Category_CategoryID_PK = Product_CategoryID_FK)  and save it ? any propositions? 

Thanks!

5 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 12 Nov 2020, 02:41 PM

Hello Wassim,

You should be able to get the CategoryID of the selected item in the select event through the e.dataItem object. You can also get the correct ID through the value method. Once you have obtained the correct value, you can initiate a call to the controller with the CategoryID as data.

If the above does not help you resolve the problem, please get back to me with a small runnable project where I can observe the issue and better understand the scenario. I will then debug it and suggest a possible solution.

Regards,
Martin
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Wassim
Top achievements
Rank 1
Veteran
Iron
answered on 12 Nov 2020, 03:44 PM

Thank you for your answer!

I resolved the problem when I'm trying to create a new poduct.

My new problem that i want to display the correct selected value of category when i'm trying to edit an existing product.

I retrived the correct values of other fileds but i don't know how to show the correct value in the dropdown list.

I'am using this stuff to display the dropdown list


                            <div class="form-group">
                                <div class="form-row">

                                @Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label col-md-2" })
                                @(Html.Kendo().DropDownList()
                                .Name("groupsDropDownList")
                                .DataTextField("CategoryName")
                                .DataValueField("CategoryID")
                                .DataSource(source =>
                                {
                                source.Custom()
                                      .ServerFiltering(true)
                                      .Type("aspnetmvc-ajax")
                                      .Transport(transport =>
                                      {
                                          transport.Read("Category_Read", "Category");
                                      })
                                      .Schema(schema =>
                                      {
                                          schema.Data("Data")
                                          .Total("Total");
                                      });
                                })
                                    )
                                </div>
                            </div>

 

any Help or suggestion?

0
Wassim
Top achievements
Rank 1
Veteran
Iron
answered on 13 Nov 2020, 09:16 AM

linked to my previous question:validation does not work in this  kendo dropdownlist   when using 

@using (Html.BeginForm("Product_Add", "Products", FormMethod.Post))      

@(Html.Kendo().DropDownListFor(model => model.CategoryID)
                                       .Name("cateoriesDropDownList")
                                       .DataTextField("CategoryName")
                                       .DataValueField("CategoryID")
                                       .DataSource(source =>
                                       {
                                           source.Custom()
                                                 .ServerFiltering(true)
                                                 .Type("aspnetmvc-ajax")
                                                 .Transport(transport =>
                                                 {
                                                     transport.Read("Category_Read", "Category");
                                                 })
                                                 .Schema(schema =>
                                                 {
                                                     schema.Data("Data")
                                                           .Total("Total");
                                                 });
                                       })
                                )

 

help please!

0
Accepted
Martin
Telerik team
answered on 16 Nov 2020, 01:12 PM

Hello Wassim,

You can use the Value configuration to set the value of a Model field. 

As per the validation, attached you will find a small example with a working validation. Let me know if it will help you resolve the problem.

If not, feel free to modify the example to demonstrate any issues you are experiencing. I will debug it and gladly assist you.

Regards,
Martin
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Wassim
Top achievements
Rank 1
Veteran
Iron
answered on 18 Nov 2020, 09:01 AM

Thanks for replying.

your answer does the trick.

Tags
DropDownList
Asked by
Wassim
Top achievements
Rank 1
Veteran
Iron
Answers by
Martin
Telerik team
Wassim
Top achievements
Rank 1
Veteran
Iron
Share this question
or