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

DropDownList value not posting to Controller

2 Answers 1189 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 24 Sep 2019, 01:19 PM

I hope this is an easy fix.  I have a DropDownList in my Grid which seems to be working fine except the selected value is not being returned to the Controller Action.  For example, when I select a new value for "Type" below, it still shows the original value in the model in the Controller.  See "<--" NOTES BELOW.

Thanks very much for the help.

*** MODEL ***
public class SelectValueViewModel
    {
        public int Id { get; set; }

        public string Code { get; set; }

        public string Description { get; set; }

        [UIHint("SelectValuesTypeDropDown")]
        public string Type { get; set; }  // <-- TRYING TO UPDATE THIS VALUE WITH THE DROP DOWN.

        public List<SelectListItem> CodeTypes { get; set; }
    }

*** VIEW ***
@(Html.Kendo().Grid<CCMC.View_Models.SelectValueViewModel>()
    .Name("SelectValuesGrid")
    .Columns(c =>
    {
        c.Bound(m => m.Code).Width(100);
        c.Bound(m => m.Description).Width(100);
        c.Bound(m => m.Type).Width(100);
        c.Command(command => { command.Edit(); }).Width(200);
        c.Command(command => command.Destroy()).Width(150);
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .Pageable()
    .Sortable()
    .Scrollable()
    //    .HtmlAttributes(new { style = "height:550px;" })
    .DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(20)
    .Model(model =>
    {
    model.Id(sv => sv.Type);
    //            model.Field(sv => sv.Id).Editable(false);
    })
    .Events(events => events.Error("error_handler"))
    .Read(read => read.Action("Read", "SelectValues"))
    .Update(update => update.Action("Edit", "SelectValues").Data("sendAntiForgery"))
    .Destroy(destroy => destroy.Action("Delete", "SelectValues"))
    )
)


*** EDIT TEMPLATE ***
@model CCMC.View_Models.SelectValueViewModel


@(Html.Kendo().DropDownListFor(m => m.Type)
            .DataValueField("Value")
            .DataTextField("Text")
            .BindTo(Model.CodeTypes)
            .BindTo((System.Collections.IEnumerable)ViewData["SelectValueTypes"])
)


*** CONTROLLER ***
[AcceptVerbs("Post")]

        [HttpPost]
        [ValidateAntiForgeryToken]


        public async Task<JsonResult> Edit([DataSourceRequest] DataSourceRequest request, SelectValueViewModel model)
        {
             model.Type  // <--- THE VALUE IS NOT UPDATED HERE.  STILL HAS THE ORIGINAL VALUE.
        }

2 Answers, 1 is accepted

Sort by
0
Chris
Top achievements
Rank 1
answered on 24 Sep 2019, 01:54 PM

Sorry, I the Custom Editor Template is as follows.  I left the following line in by accident:  .BindTo(Model.CodeTypes).  Thanks for your help.

 

@model CCMC.View_Models.SelectValueViewModel

@(Html.Kendo().DropDownListFor(m => m.Type)
            .DataValueField("Value")
            .DataTextField("Text")
            .BindTo((System.Collections.IEnumerable)ViewData["SelectValueTypes"])
)

0
Chris
Top achievements
Rank 1
answered on 24 Sep 2019, 11:15 PM

I figured it out.  The fields need to be in the model declaration of the grid.

  .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Model(model =>
        {
            model.Id(p => p.Id);
            model.Field(p => p.Id).Editable(false);
            model.Field(p => p.Type);
            model.Field(p => p.Description);
            model.Field(p => p.Code);
        })

Tags
DropDownList
Asked by
Chris
Top achievements
Rank 1
Answers by
Chris
Top achievements
Rank 1
Share this question
or