Kendo DropDownListFor () with ASP.NET-MVC

I have a problem with the ASP.NET-MVC helper I have a form that gives POST in action ** creates an Occurrence controller by passing an entry type parameter that matches the View Model in which the form is pasted , registration requires a TypeOccurrenceID event, I try to get this value using Html.DropDownListFor () , but this does not work when submitting a form, the past Origin in the parameter does not have an OccurrenceTypeId corresponding to the OccurrenceType method selected in DropDownList

Has anyone had the same problem?

This is my action with the controller

[HttpPost] public ActionResult Create(Occurrence occurrence) { if (ModelState.IsValid) { try { db.Add<Occurrence>(occurrence); return new HttpStatusCodeResult(200); } catch (Exception) { return new HttpStatusCodeResult(400); } } return new HttpStatusCodeResult(400); } 

Here is my look

 @using Common.Util @using Common.Util.Configuration @using CafData @model Occurrence <div class="box-form"> @using (Ajax.BeginForm("Create", "Occurrence", new AjaxOptions { OnSuccess = "OnSuccess()", OnFailure = "OnFailure()" })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) @*Area*@ <div class="row-fluid details-field"> @(Html.Kendo().DropDownList() .Name("areas") .HtmlAttributes(new { style = "width:300px" }) .OptionLabel("Selecione uma area...") .DataTextField("Description") .DataValueField("IdArea") .DataSource(source => { source.Read(read => { read.Action("readAreasForDropDown", "Area"); }); }) ) @*Occurrence type*@ @(Html.Kendo().DropDownListFor(m => m.OccurrenceTypeId) .Name("occurrencetype") .HtmlAttributes(new { style = "width:300px" }) .OptionLabel("Select a occurrence type...") .DataTextField("Description") .DataValueField("OccurrenceTypeId") .DataSource(source => { source.Read(read => { read.Action("lerOccurrenceTypeForDropDown", "OccurrenceType").Data("filterArea"). Type(HttpVerbs.Post); }) .ServerFiltering(true); }) .Enable(false) .AutoBind(false) .CascadeFrom("areas") ) <script> function filterArea() { return { id: $("#areas").val() }; } </script> <button class="k-button">Save</button> } </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") } 

Sorry for the bad english!

+6
source share
1 answer

The problem was that the name of the dropdown should be the same name as the property of the model you want to bind.

Example:

  @(Html.Kendo().DropDownListFor(m => m.OccurrenceTypeId) .Name("OccurrenceTypeId") 

Alternative:

The name property is not actually required when using DropDownListFor. So just deleting this line will work:

  .Name("occurrencetype") 
+17
source

Source: https://habr.com/ru/post/946081/


All Articles