My Model
namespace MDT.Models
{
public class Vehicle
{
[Key]
public int VehicleID { get; set; }
public int UserID { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
public int MakeID { get; set; }
public int ModelID { get; set; }
[DisplayName("Color")]
public int ColorID { get; set; }
[Required]
[StringLength(4)]
public string StickerKey { get; set; }
[DefaultValue(true)]
public Boolean IsActive { get; set; }
public virtual CarColor CarColor { get; set; }
}
}
My Controller action for the udpate
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Vehicle vehicle)
{
if (ModelState.IsValid)
{
db.Entry(vehicle).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(vehicle);
}
<
div
class
=
"editor-label"
>
@Html.LabelFor(model => model.ColorID)
</
div
>
<
div
class
=
"editor-field"
>
@*@Html.EditorFor(model => model.ColorID)*@
@Html.ValidationMessageFor(model => model.ColorID)
@(Html.Kendo().DropDownListFor(model => model.ColorID)
.BindTo(ViewData["CarColors"] as SelectList)
.Name("kendocarcolor")
.Value(Model.ColorID.ToString())
.OptionLabel("Select a Color")
)
</
div
>
When I perform the save for the above, the ColorID comes back at zero. Shouldn't it be getting passed back with the updated model for the post? I also have noticed that my validator for the ColorID is not working for the Kendo Dropdown list but if I uncomment the EditorFor, it does work on that control.
If you could please help me with the following it would be greatly appreciated:
1) Why doesn't the dropdownlist come back with my updated model value to my controller?
2) Any ideas why the validator doesn't work?
3) Did I populate the dropdownlist the most efficent way using ViewData? How would you do it?
THANK YOU very much in advance.
15 Answers, 1 is accepted
Widget's value is not bound to the ColorID property, because its name is overriden using this line:
.Name(
"kendocarcolor"
)
Regards,
Georgi Krustev
Telerik
Any ideas on that one?
I am not exactly sure where could be the cause of the issue. If both the EditorFor extension method and the Kendo DropDownListFor are used on the page then only the first one will get the unobtrusive data annotation attributes. I suppose that this is not the case at your implementation. Hence I will need a simple test project, which reproduces the issue. Thus I will check the problem locally and advice you further.
Georgi Krustev
Telerik
My problem is on the create vehicle, if you leave the dropdown blank you will get an error and no validation is happening what so ever.
THANK YOU.
Thank you the attached files. Unfortunately, I was not able to get the project to work. One assumption about the missing validation. If you are using jQuery Validate framework (I was not able to find any jquery.validate.js though) then you will need to set ignore option to empty string, as it does not validate hidden elements since version 1.9.0:
[_Layout.cshtml]
<script>
$.validator.defaults.ignore =
""
;
</script>
If this is not the case the I will ask you to send us a simple runnable test project. Thus we will be able to investigate the case locally and find a resolution to the problem fast.
Regards,
Georgi Krustev
Telerik
https://www5.newportbeachca.gov/webmedia/kendomdt.zip
To duplicate my problem:
1) Run the project
2) Click on Vehicles in the top menu (that should take you to the create screen)
3) Try to submit the form without entering in any values.
The Kendo Dropdownlist seems to kill validation for any other control as well as pull an error without doing any server side validation. My understanding is that server side validation should at least happen even if the client side is not happening. I'm sure it's something stupid that I'm doing wrong. Thank you!
Thank you for the attached test project. I was able to run it and it actually worked as expected. The page loads correctly and on post, the server validation reports that the model is not valid. Check this screencast for more information. Probably I am missing something?
Regards,
Georgi Krustev
Telerik
Can you paste your code?
Thanks.
Regards,
Hristo Valyavicharski
Telerik
<
p
>I am Creating Cascade dropdownlist its working well but its Giving ProvinceId 0 to my controller </
p
> <
p
>@(Html.Kendo().DropDownListFor(model => model.ProvinceId)<
br
>
.OptionLabel("--Select Province--")<
br
>
<
br
>
.HtmlAttributes(new { style = "width:283px" })<
br
>
.DataTextField("ProvinceName")<
br
>
.DataValueField("ProvinceId")<
br
>
<
br
>
.DataSource(source =><
br
>
{<
br
>
source.Read(read =><
br
>
{<
br
>
read.Action("GetCascadeProvince", "Student")<
br
>
.Data("filterCountries");<
br
>
}).ServerFiltering(true);<
br
>
})<
br
>
.Enable(false)<
br
>
.AutoBind(true)<
br
>
.CascadeFrom("CountryId")<
br
>
)</
p
><
br
>
Could you elaborate more on the following points:
- Which particular action in the controller are you trying to access ProvinceId in? Could you post the action's declaration?
- How is this action called?
- Could you post your filterCountries function? This will show us the parameters you are sending to the GetCascadeProvince action.
Regards,
Ivan Danchev
Progress Telerik
Is there some reason why the model (ienumerable) does not pass back to the controller the selected value
Here is the controller definition:
[HttpPost]
public ActionResult FEEmployee_Read(List<SelectPayWeek>SelectedPayWeek)
View:
@model IEnumerable<TimeSheetEntryForManagers.Models.SelectPayWeek>
@*@model IEnumerable<MvcApplication1.Models.Product>*@
@{
ViewBag.Title = "Select Pay Week";
}
<script type="text/javascript">
</script>
<style>
#payweekDropDownList {
width: 1000px !important;
}
</style>
@using (Html.BeginForm("FEEmployee_Read", "FETimeEntry", FormMethod.Post))
//, new { payweek = Model, onsubmit = "validate()" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div>
<h4> Select a pay week </h4>
@(Html.Kendo().DropDownList()
.Name("ddlPayWeek")
.DataTextField("PayWeekStartDate")
.DataValueField("TMPayWeekID")
.BindTo(Model)
.SelectedIndex(0)
.HtmlAttributes(new { style = "width: 100%" })
)
Are you able to pass other field values or the value of the standard MVC @Html.DropDownList helper to the controller or you have an issue only with the Kendo UI DropDownList? See the following thread which demonstrates the DropDownList's configuration, how its value is submitted with the form and how it can be accessed in the controller.
Regards,
Ivan Danchev
Progress Telerik