Is there a way to find the Grid Name in the Controller?
I am defining the grid name in my page.
<%:
Html.Telerik().Grid<Scout.Server.UI.Web.Mvc.ViewModels.WTO.WTOListViewModel>()
.Name("GridName")
.DataKeys(keys => keys.Add(wto => wto.WTORowID))
.DataBinding(dataBinding => dataBinding.Ajax()
.Select("QueryMyGrid", "GridController")
)
.Columns(columns =>
{
I need to find the Grid name in my Action Method.
[GridAction]
public ActionResult QueryMyGrid(GridCommand command)
{
var transferOrders = transferOrderService.GetActiveTransferOrdersBySubType(
typeService.GetSubTypeByMeaning(ModelDefinitions.TypeClassMeaning.ORDER_TYPES,
ModelDefinitions.TypeMeaning.ORDER_TYPE_TRANSFER_ORDER,
Is there a way to do that?
Why do you need the Grid name ?
When you use Ajax DataBinding, the Grid will call the specified action (Grid/QueryMyGrid) and fill the grid with the returned object.
You need to return something like this
return View(new GridModel<Scout.Server.UI.Web.Mvc.ViewModels.WTO.WTOListViewModel>(transferOrders));
Related
I am loading some ids to a ViewBag to use it's data in the related View.
var userAccountList =serviceResponse.RoleList.Select(x => x.RoleId).ToList();
ViewBag.UserRoles = userAccountList;
this result is a List<string>.
In the View a Kendo Grid retrieves data from json.
#(Html.Kendo().Grid<RoleModel>().Name("KendoGrid").Scrollable().Columns(columns =>
{
columns.Bound(x => x.Id)
.HtmlAttributes(new { style = "text-align:center;"})
.ClientTemplate("# if ( Id != '3'){
#<a class='k-button' href=\"/Role/Delete/#= Id #\"><span class='k-icon k-delete' style='pointer-events: none;'></span>Delete</a># }#")
.Width(85).Sortable(false)
.Hidden(!((BaseController)this.ViewContext.Controller).UserHasPermission(PermissionType.RoleDelete));
columns.Bound(x => x.Name);
....
}
When I use static values such as '3' I could prevent to show a tag that was used as delete button. How can I use ViewBag.UserRoles in this conditional if the list contains Id that's bound to column?
Generally in order to use Razor you would have to do something like that
.ClientTemplate("# if ( Id != "'+ #ViewBag.UserRole +'")
If we were talking about one string value.
Now for your case you could use Template instead of ClientTemplate with something like this
.Template(
#<text>
#if (ViewBag.UserRoles.Any(u => u == item.Id))
{
... add your link
}
</text>
A couple more examples you can find in this post.
Most probably it has documentation too by I couldn't find what I was looking for at the moment.
Telerik MVC grid column not identified until I add
#model IEnumerable<NTI.Data.EDC.LabUnit> as first line of my view. However when I do add this line.
Telerik MVC Grid loads with data and also shows ups all buttons. But Insert, Update and delete events not getting triggered. There is no Javascript error in the browser console window. And I also noticed there is no form action for Insert/Update/Delete buttons created by telerik. Please help.
My code ref: http://www.telerik.com/help/aspnet-mvc/telerik-ui-components-grid-editing-ajax-editing.html
View
#(
Html.Telerik().Grid(Model)
.Name("Grid")
.DataKeys(dataKeys => dataKeys.Add( c.ID))
.ToolBar(commands => commands.Insert())
.DataBinding(dataBinding => dataBinding
//Ajax binding
.Ajax()
//Home.Index renders the grid initially
.Select("LabUnits", "Lab")
//Home.Insert inserts a new data record
.Insert("LabUnitsInsert", "Lab")
//Home.Update updates an existing data record
.Update("LabUnitsUpdate", "Lab")
//Home.Delete deletes an existing data record
.Delete("LabUnitsDelete", "Lab")
)
.Columns(columns =>
{
columns.Bound(c => c.ContactName);
columns.Bound(c => c.Country);
columns.Bound(c => c.BirthDay);
columns.Command(commands => commands
.Edit()
.Delete());
})
)
Controller
public class LabController : Controller
{
public ActionResult LabUnits()
{
IEnumerable<LabUnit> lbUnit = new LabUnitDB().SelectAll();
return View(new GridModel(lbUnit));
}
[HttpPost]
[GridAction]
public ActionResult LabUnitsInsert()
{
//insert
}
[HttpPost]
[GridAction]
public ActionResult LabUnitsUpdate(int id)
{
//update
}
[HttpPost]
[GridAction]
public ActionResult LabUnitsDelete(string id){
// Delete
}
}
I was able to fix the issue. Had to replace
.DataBinding(dataBinding => dataBinding
//Ajax binding
.Ajax()
with
.DataBinding(dataBinding => dataBinding
//Server binding
.Server()
So its server binding and not Ajax.
I can't use edit and create command of a Kendo Grid, because binding always fails and controller always receives a null object, even though DataSourceRequest is fine.
I used a #html.EditorForModel() and it worked fine and controller received the data. So MVC Model binding and my class aren't the problem.
Also, using F12, I can see the data being posted and it's fine too.
Any Idea about possible problem and debugging? There is an issue with all of my Kendo Grids, sometimes they post a model twice and I receive double Create on the server, which one of them always fails because of repetitive Keys. Or sometimes create will be triggered by edit command too.
I have no idea where I'm going wrong, in most scenarios the user will choose the primary key as well, so my returned model doesn't need an update.
With this particular class, server always receives two create and none of them binds correctly. The first create, posts all of the fields, and the second posts all of the navigation properties' fields too!
This is the action method:
[HttpPost]
public ActionResult Create([DataSourceRequest] DataSourceRequest request, Stock stock){
if (stock != null && ModelState.IsValid)
{
repo.Insert(stock);
return Json(new[] { stock }.ToDataSourceResult(request, ModelState));
}
else
return Json(null);
}
And the Grid:
#(Html.Kendo().Grid<Stock>()
.Name("stock")
.Columns(columns =>
{
columns.ForeignKey(x => x.CGoodCode, (IEnumerable)ViewData["Goods"], "Id", "Text");
///
columns.Command(command => { command.Edit(); command.Destroy(); });
})
.ToolBar(toolbar => {
toolbar.Create();
toolbar.Excel();
toolbar.Pdf();
})
.Editable(edit => edit.Mode(GridEditMode.PopUp))
.Sortable()
.Pageable(p => p.PageSizes(true))
.DataSource(ds =>
ds.Ajax()
.Model(model => {
model.Id(x => x.CGoodCode);
model.Id(x => x.SWhCode);
model.Id(x => x.LiIdPeriod);
model.Field(x => x.CGoodCode).DefaultValue(ViewData["GoodsDefault"]);
////
})
.Read("Read", "Stock")
.Update("Edit", "Stock")
.Create("Create", "Stock")
.Destroy("Delete", "Stock")
)
)
UPDATE
It seems Kendo Grid doesn't support composite primary keys yet!
This question helped me out! I just need to rename the parameter like:
public ActionResult Create([DataSourceRequest] DataSourceRequest request, Stock somethingElse){
Because my class contains some fields with that name, Stock!
I have a Kendo ComboBox as an EditorTemplate for nullable IDs (people). I bind the model normally with a UIHint
#Html.Kendo().ComboBoxFor(x=>x.Person.MotherID)
The combo works great, I can type and it filters on the server.
The problem is that when the form first loads, the ID of the person appears as the visible text, not the name.
There are 1 million records, so I can't send them all to the combobox initially.
How do I get the combobox, when it first renders, to go get the name of the person via Ajax and not show the ID?
#(Html.Kendo()
.ComboBoxFor(m => m)
.DataTextField("Name")
.DataValueField("Value")
.AutoBind(true)
.ValuePrimitive(true)
.Filter(FilterType.Contains)
.Placeholder(Words.Type_Name)
.MinLength(2)
.DataSource(source =>
source.Read(read =>
read.Action("ReadComboBoxPerson", "Picker", new { area = "" }))
.ServerFiltering(true))
)
MVC:
public ActionResult ReadComboBoxPerson(string text)
{
return Json(_personManager.PersonGetter(text), JsonRequestBehavior.AllowGet);
}
I want to build one template with telerik grid, which is invoking by other page as an partial view.
the template code like this:
#model IEnumerable<object>
#using System.Collections;
#using System.Collections.Generic;
#using System.Reflection;
#{
ViewBag.Title = "UserView2";
}
<div>
#{Html.Telerik().Grid(Model)
.Name("UserConfiguration")
.DataKeys(keys => keys.Add(o => o.GetType().GetProperty("ConfigurationItemId").GetValue(o)))
.Columns(
columns =>
{
Type t = Model.GetType().GetGenericArguments().FirstOrDefault();
foreach (var prop in t.GetProperties())
{
columns.Bound(prop.PropertyType, prop.Name);
}
columns.Command(commands => commands.Edit()).Width(100);
}
)
.DataBinding(
dataBinding => dataBinding.Server()
.Update("UpdateUser", "Configuration")
)
.Pageable(paging => paging.PageSize(20))
.Sortable()
.Scrollable(o => o.Height(500))
.Filterable()
.Resizable(resizing => resizing.Columns(true))
.Render();
}
The other view page Invoke the template view like this:
#Html.Partial("UserView2", Model.UserList)
And when i run it, the page open successfully and display all the data successfully. so i think it works well through reflecting get the columns name and columns data. But when i click "Edit" button, it will throw an exception:
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
anyone can help me solve this?