Reputation: 13
I'm trying to use mvc kendo ui but i have problem when using with strongly typed view, when submit form kendo control send null or zero value to controller !! anothe html helper control send correct value . i will set code example of my work with combobox .
Model:
public class Sys_Branches
{
public int id { get; set; }
[Required]
[ForeignKey("Companyid")]
public Sys_Companies Sys_Companies { get; set; }
public int Companyid { get; set; }
public string Code { get; set; }
[Required(ErrorMessage = "Name required")]
public string NameA { get; set; }
[Required(ErrorMessage = "Name required")]
public string NameE { get; set; }
}
View:
@model MVC_ERP.Models.Sys_Branches
@{
ViewBag.Title = "Branches";
}
<div class="col-xs-12 col-sm-2">
@Html.Label("Company")
</div>
<div class="col-xs-12 col-sm-10" style="margin-bottom: 5px">
@(Html.Kendo().DropDownListFor(model => model.Notes).Name("Companyid")
.DataTextField("NameE")
.DataValueField("id")
.DataSource(source => source
.Read(read => read
.Action("FillCompanies", "Branches")
)
)
)
</div>
Controller"
public ActionResult Index(int? id, string submitButton, Sys_Branches MainTbl)
{
//New or edit according id pass if 0 new else edit
if( ModelState.IsValid)
{
if (Insert(MainTbl)) return RedirectToAction("Index", new { id = MainTbl.id
}
//Model not valid
return View(MainTbl);
}
public ActionResult FillCompanies([DataSourceRequest]DataSourceRequest request)
{
Session["MemberShipId"] = 1;
int MemberShipId =Convert.ToInt32(Session["MemberShipId"]);
var m = db.Companies.Where(b => b.MemberShipId == MemberShipId && b.Deleted != true);
//return Json(m.Select(p => new { Companyid = p.id, NameE = p.NameE }), JsonRequestBehavior.AllowGet);
return Json( m.AsQueryable(), JsonRequestBehavior.AllowGet);
}
Upvotes: 0
Views: 1288
Reputation: 141
Your Dropdown is binded from "Notes" property, and Name is "Companyid". Your class "Sys_Branches" does not contain "Notes" property.
Use the following code :
@(Html.Kendo().DropDownListFor(model => model.Companyid).Name("Companyid")
.DataTextField("NameE")
.DataValueField("id")
.DataSource(source => source
.Read(read => read
.Action("FillCompanies", "Branches")
)
)
)
On Submit, value will be set in "Companyid" property of your class.
Upvotes: 0