I’m using MVC Kendo grid and few drop down lists as filters and search button. Grid is bound using Ajax. The filters are defined outside of the grid. When user clicks on the search button i am passing the selected filter values to action method as Model, do the search and return the result. This is working fine when user selects the filter.
However, One of the filter is required filter, so I have [Required] attribute on the model’s property. When user don’t select the required filter, it makes the GetData() action method as expected, I see ModelState is NOT valid as expected.
But here im not sure what should I return when Model is not valid,
Questions:
I have validation summary on the layout page, how do I show model error in ValidationSummary without clearing the grid?When action method returns error, I don’t want to clear the existing result in the Grid. The error should display on top of the page.
Note: For brevity purpose my sample below showing only one filter. Also I’m not worrying about client side validation.
Layout Page
<html><head> @Styles.Render("~/Content/css") @Styles.Render("~/Content/kendo/css") @Scripts.Render("~/bundles/modernizr") @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/kendo") @Scripts.Render("~/bundles/bootstrap") @Scripts.Render("~/bundles/jqueryval")</head><body> <div class="container body-content"> @Html.ValidationSummary() @RenderBody() <footer></footer> </div> @RenderSection("scripts", required: false)</body></html>Index Page
@model MyModel @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Index</h2> <div> <br /> @Html.ListBoxFor(x => x.SelectedIDs, new MultiSelectList(Model.States, "ID", "Name")) @Html.ValidationMessageFor(m => m.SelectedIDs) </div> <input id="btnSearch" type="submit" width="100" value="Search" /> @(Html.Kendo().Grid<Person>() .Name("grid") .Columns(col => { col.Bound(p => p.ID); col.Bound(p => p.Name); }) .DataSource(ds => ds.Ajax() .Read(r => r.Action("GetData", "Working")) .PageSize(3)) .Pageable() .Sortable() .AutoBind(false) ) <script src="~/Scripts/Working.js"></script>
JavaScript
$(function () { var myModel = { SelectedIDs: [] }; var dataSource = $("#grid").data("kendoGrid").dataSource; dataSource.transport.options.read.data = getFilters; $("#btnSearch").click(function () { myModel.SelectedIDs = $("#SelectedIDs").val(); $("#grid").data("kendoGrid").dataSource.read(); $("#grid").data("kendoGrid").refresh(); }) function getFilters() { return myModel; } })Controller
public class WorkingController : Controller { public ActionResult Index() { var model = new MyModel(); return View(model); } public ActionResult GetData([DataSourceRequest]DataSourceRequest request, MyModel model) { if (!ModelState.IsValid) { // What should i return here so that Model error message will appear in ValidationSummary ??? } return Json(MockData.GetPersons().ToDataSourceResult(request), JsonRequestBehavior.AllowGet); } }
Model
public class MyModel { [Required(ErrorMessage = "State is required.")] public int[] SelectedIDs { get; set; } public IEnumerable<State> States { get { return MockData.GetStatesWithAbbr(); } } public IEnumerable<Person> Persons { get; set; } }01.Html.Kendo().Grid<dynamic>()02. .Name("grid")03. .Filterable(filter => filter.Enabled(false))04. .DataSource(ds => ds.Ajax().Read(read => read.Action("action", "controller")))05. .Columns(columns =>06. {07. columns.Bound("field")08. .Title("title")09. .Sortable(true)10. .HeaderTemplate("???");11. });
Hi All
I'm trying to start using my license, but I'm doing something wrong for sure.
I copy to my page of a project that is working, the following script from a new project create thru template Telerik \Web\c# ASP.NET MVC Application, from Index.cshtml. (@(Html.Kendo().PanelBar()..."
But I don't have any change, I move the mouse over the bar, and I saw the new bar, but nothing happen when I click over that position.
I suppose is missing some bundles, but I don't know what js is missing.
In the new project everything works well, but not in my.
Any clue what I must do?
Paulo Afonso
Hi,
I am using the ASP.NET MVC Grid control on a page. The functionality is to fire a AJAX query when a Grid row is selected. The AJAX query calls the MVC Controller and gets a response HTML.
The response HTML is a Tab Strip with 4 tabs , all of them are empty except for one which shows a hierarchy (built using <ul> and <li> tags).
Scenario,
Issue
Once the AJAX response is added to the DOM, the GriD is no longer accessible. None of the rows are selectable anymore, the below line returns null
var grid = $('#MasterGrid').data("kendoGrid");
Any help appreciated.
-Harris
Hi there,
I am using the MultiSelectFor control bound to a model property in a MVC 5 razor webpage. I am able to select multiple items and create data in our backend system - however, I am running into a few problems when editing this data.
Model property:
[DataType("MultiSelect")][Required]public List<string> Teams { get; set; }
Editor Template:
@model IList<string>@Html.Kendo().MultiSelectFor(m => m) .DataTextField("Text") .DataValueField("Value") .Filter("contains") .AutoClose(false) .Placeholder("Please Select") .ValuePrimitive(true) .Deferred() .BindTo(selectList) .ToClientTemplate();The list it is bound to is an IEnumerable<SelectListItem>
When the screen loads, everything appears fine - the multiselect field is bound to the correct items and displays correctly.
However, if I attempt to remove one of the selected items by clicking on the delete option, I receive a javascript error in kendo.all.min.js:
0x800a138f - JavaScript runtime error: Unable to get property 'top' of undefined or null reference
The multiselect then appears to have lost it's list of values, which will only reappear if I start typing.
Any ideas what could be the issue here? If I expand the list first prior to deleting, I receive no issues when removing a selected item.
Thanks,
Paul.
I am building an application in ASP.NET MVC where I am building different Kendo charts to show realtime data with SignalR. I am trying to let the Hub push data constantly to the chart. But I only manage to let the chart update its data on page load, not realtime.
<script src="@Url.Content("~/Scripts/jquery.signalR-2.2.0.min.js")"></script><script src="@Url.Content("~/signalr/hubs")"></script><script> var hub, hubStart; $(function () { var hubUrl = "http://localhost:52373/signalr/hubs"; var connection = $.hubConnection(hubUrl, { useDefaultPath: false }); hub = connection.createHubProxy("serverHub"); $.connection.hub.logging = true; hubStart = connection.start(); });</script>
@(Html.Kendo().Chart<Model.ServerState>() .Name("server1") .Title(title => title .Text("Server 1") .Color("White") .Font("25px Arial") ) .Legend(legend => legend .Color("Gray") .Position(ChartLegendPosition.Bottom) .Font("20px Arial") ) .ChartArea(chartArea => chartArea .Background("transparent") ) .SeriesDefaults(seriesDefaults => seriesDefaults.Line().Style(ChartLineStyle.Smooth) ) .Series(series => { series.Line(model => model.Performance.Memory).Name("Memory"); series.Line(model => model.Performance.Processor).Name("Processor"); }) .CategoryAxis(axis => axis .Title(categoryTitle => categoryTitle .Text("Time") .Font("25px Arial")) .Color("Gray") .Labels(labels => labels .Font("20px Arial")) .MajorGridLines(lines => lines.Visible(false)) ) .ValueAxis(axis => axis .Numeric().Labels(labels => labels.Format("{0}%")) .Line(line => line.Visible(false)) .AxisCrossingValue(-10) .Color("Gray") .Labels(labels => labels .Font("20px Arial")) ) .Tooltip(tooltip => tooltip .Visible(true) .Format("{0}%") ) .DataSource(source => source .SignalR() .AutoSync(true) .Transport(transport => transport .Promise("hubStart") .Hub("hub") .Client(c => c .Read("read")) .Server(server => server .Read("read") )) .Schema(schema => schema .Model(model => { model.Id("Id"); model.Field("Id", typeof(int)).Editable(false); model.Field("Memory", typeof(float)); model.Field("Processor", typeof(float)); } ) )))
//Best case: public ServerStateEvent Read() { //Inside thread which pushes an message containing the object to kendo chart Clients.All.sendMessage(msg); }//Current case, this method has to be called by kendo chart to update its data realtime public ServerStateEvent Read() { //this method gets data and returns it to kendo chart return msg; }What I want is the "best case" where the Hub pushes data with the sendMessage function to kendo. This does not seems possible?
The current case works but only one time, the autoSync in kendo does not help.
I am using a foreign key column in my grid, and populate the data from viewdata as below
columns.ForeignKey(c => c.PersonId, (SelectList)ViewData["PersonSource"]);
I set the ViewData["PersonSource"] at the page load and the values are updated correctly.
But in UI the column populates the values from the cache rather than the ViewData. It works fine if I manually clear the browser cache.
Anyways I can overcome this issue?
<div class="span3"> @(Html.Kendo().MultiSelect() .Name("tags") .Placeholder("No tags selected for this unit") .BindTo(new SelectList(Model.TagsAvailable)) .Events(e => e .Select("select") .Change("change")) .Value(Model.TagsSelected.ToArray()) )</div><script> function select(e) { var dataItem = this.dataSource.view()[e.item.index()]; var param = dataItem.Text; var url = '@Url.Content("~/UnitDetails/TagUnit/" + Model.UnitId)'; $.ajax({ url: url, data: { selectedItem: param }, type: 'GET', dataType: 'json', success: function (data) { }, error: function () { } }); }; function change(e) { var dataItem = this; var param = dataItem.element.context.innerText; var url = '@Url.Content("~/UnitDetails/UnTagUnit/" + Model.UnitId)'; $.ajax({ url: url, data: { selectedItem: param }, type: 'GET', dataType: 'json', success: function (data) { }, error: function () {
} }); };</script>