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

Kendo DropDownList in MVC 4

1 Answer 139 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
pineapple
Top achievements
Rank 1
pineapple asked on 14 Feb 2015, 09:17 AM
Dear Sir:

I would like to ask a question in kendo controls

when
I filter grid the dropdown auto post back and it doesn't stop on
selected value that i have chosen, so please I need your advice in it.

and here's my Code:
Razor:
@using Kendo.Mvc.UI;
@model IEnumerable<OfferEntityFrameworkDB.OfferDetail>
@{
    ViewBag.Title = "List";
}
<h2>List</h2>

<div>
    @Html.Label("Language:")
</div>
<div>

    @(Html.Kendo().DropDownList()
    .Name("ddlLang")
   .OptionLabel("Please Select Language ...")
    .HtmlAttributes(new { style = "width:200px;" })
    .DataTextField("Name")
    .DataValueField("ID")
    .DataSource(source => source
    .Custom()
    .Transport(transport => transport
    .Read(read =>
      {
          read.Url("ComboRead");
      })).ServerFiltering(false)
      )
        .AutoBind(false)
      .Events(e =>
            {
                e.Change("onChange");
            })
  )

</div>

<br />
<br />
<div>
    @(Html.Kendo().Grid((IEnumerable<OfferEntityFrameworkDB.OfferDetail>)ViewData["OfferData"])
        .Name("grdOffers")
        .Columns(columns =>
        {
            columns.Bound(o => o.OfferID).Hidden();
            columns.Bound(o => o.LanguageID).Hidden();
            columns.Bound(o => o.Name);
            columns.Bound(o => o.Brief);
            columns.Bound(o => o.Desc);
           
columns.Template(c => @Html.ActionLink("Edit", "Edit", new { langId =
c.LanguageID, offerId = c.OfferID })).Title("Edit");
           
columns.Template(c => @Html.ActionLink("Delete", "Delete", new {
langId = c.LanguageID, offerId = c.OfferID })).Title("Delete");
        })
      )

</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
     <script type="text/javascript">
        $(document).ready(function () {
            $("#ddlLang").change(function () {
                var langId = $("#ddlLang").data().kendoDropDownList.value();
                alert(langId);
                var grid = $("#grdOffers").data().kendoGrid;
                if (langId != null || langId != new Guid("")) {
                    grid.dataSource.filter({ field: "LanguageID", operator: "eq", value: langId });
                }
                else {
                    grid.dataSource.filter({});
                };
            });
        });
    </script>
}

Controller:
 public ActionResult List()
        {
            List<OfferDetail> lstOfferDetails =  db.OfferDetails.ToList();
            ViewData["OfferData"] = lstOfferDetails;
            return View(ViewData["OfferData"]);
        }
 public JsonResult ComboRead()
        {

            var lstlanguages = from lan in db.Languages select new { Name = lan.Name, ID = lan.ID };

            return Json(lstlanguages, JsonRequestBehavior.AllowGet);

        }


even sever Controller not working properly when I add data bind to server in the grid.

        List<OfferDetail> lstoffers;
        [HttpPost]
        public JsonResult GetOffersByLangID(Guid? langId)
        {
            lstoffers = (from o in db.OfferDetails where o.LanguageID == langId select o).ToList();
            return Json(lstoffers, JsonRequestBehavior.AllowGet);
        }

Best Regards,

1 Answer, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 18 Feb 2015, 06:58 AM
Hello,

Based on your explanation and code it seems that you want to refresh the Grid with new data from the server and send that the selection of the DroPDownList to the server. To do so you need to send this value as a regular parameter, instead of trying to filter the Grid by that value.

How to send extra parameter to the server and refresh the Grid is covered here:

http://docs.telerik.com/kendo-ui/aspnet-mvc/helpers/grid/faq#how-do-i-send-values-to-my-action-method-when-binding-the-grid

Kind Regards,
Petur Subev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
DropDownList
Asked by
pineapple
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Share this question
or