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.
Related
I have a kendo mvc grid set for inline editing. I have a dropdown that I would like to populate when the user selects edit, and the list needs to be populated based upon a value in the row (which is in a hidden field)
c.Bound(a => a.MyId)
.Hidden(true);
I have the dropdown that I wish to populate in an editor template (which I need as this dropdown is shared by several grids).
#model int?
#(Html.Kendo().ComboBoxFor(m => m)
.AutoBind(true)
.Filter("contains")
.Placeholder("Select...")
.DataTextField("MyOtherName")
.DataValueField("MyOtherId")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("Data_Read", "MyController").Data("OnAdditionalData");
});
})
.Suggest(true)
.HtmlAttributes(new { data_value_primitive = "true" })
)
#Html.ValidationMessageFor(m => m)
With an additional data method in a script file ...
function OnAdditionalData()
{
var myId = $("#MyId").val();
return { myId : myId };
}
I would like for the dropdown to populate when the user clicks on the row edit button. With the autobind property set to true, it does just that. But the value in the additional datat function is always an empty string. This value does not populate correctly until after the additional data method (verified using the JS console.) If I set autobind to false, the user has to click on the drop down in the editable row, but then the value of myId is correct.
I see what is going on, with autobind true the values from the row object is not being populated until after the read method of the ComboBox is called.
Does anyone know of a work around for this?
I use a Kendo grid to show my table fields like this:
#(Html.Kendo().Grid<MyProject.Admin.Models.MyViewModel>(Model)
.Name("id")
.Columns(columns =>
{
columns.Bound(c => c.ID).Filterable(false);
columns.Bound(c => c.ModelNo).Filterable(false);
It works fine but one of the fields is userId , a GUID field and I want to call a static method which returns Username. I've already created this method. The method is
public static string GetUserName(Guid UserId)
{
var db = new ApplicationDbContext();
var user = (from k in db.Users where k.Id == UserId.ToString() select k).FirstOrDefault();
if(user != null)
{
return user.UserName;
}
else
{
return "-";
}
}
So what I need is to call this method in Kendo Grid, something like:
columns.Bound(c => c.UserID).Template(#<text>
<strong>#Tools.GetUserName(c.UserID)</strong>
</text>);
columns.Bound(p => p.Title).Template(#<text>
<strong>#item.Title</strong>
</text>);
simply generates javascript like this one
columns: [{
...
template: "<strong>#:Title#</strong>"
}],
And template: "<text><strong>#:Title#</strong></text>" further processed by templating engine in the Kendo UI Framework at client side. Therefore replacing #:Title# to any C# function wouldn't be recognized at browser end.
Hi I have included Grid.MVC in asp .net mvc 4 i am able to display the
checkbox in grid but how do i get to know the particular checkbox is
selected in a row since i want to post it to the server and check it
at the controller end Below is the code any advice regarding are
welcome and is there any way i can place a checkbox for header
#Html.Grid(Model).Columns(columns =>
{
columns.Add().Encoded(false).Sanitized(false).SetWidth(10).RenderValueAs(o => Html.CheckBox("checked", false));
columns.Add().Encoded(false).Sanitized(false).Titled("Headline").SetWidth(220).RenderValueAs(news => #Html.ActionLink(news.HeadLine, "Details", new { Id = news.Id }));
columns.Add(news => news.Time).Titled("News Time").SetWidth(110).Sortable(true).Filterable(true);
}).WithPaging(20)
Sorry for the late reply, but today I found my self with the same question.
The following code will work as you want:
columns.Add()
.Encoded(false)
.Sanitized(false)
.SetWidth(10)
.RenderValueAs(o => Html.CheckBox("checked", o.YourModelBoolProperty));
Good luck.
Add value attribute to the checkboxes, set their value as news.Id (if news.Id is the primary key or unique else set some value by which you will be able to identify the checked rows in controller).
Add the name attribute to the checkboxes and give same name to all of them.
Html.CheckBox("checked", false, new {name = "assignChkBx"})
Use a action in controller with form colection object to get the checkbox values
public void Controls(FormCollection form)
You will get only those values in FormCollection which are checked.
var checkBox = form.GetValues("assignChkBx");
if (checkBox != null)
{
foreach (var id in checkBox)
{
}
}
columns.Add().Titled("").Encoded(false).Sanitized(false).RenderValueAs(o => Html.CheckBox(string.Format("ArrayProperty[{0}].Selected", Model.ArrayProperty.IndexOf(o)), o.Selected));
Works for me - and a little bit cleaner than the other answer. For some reason I can't get the other item posted to work
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));
my application is asp.net MVC, I am using Telerik MVC Combobox, using ViewData, I send items as:
people.Add(new Person { Id = 1, Name = "John", viewed = true });
<% Html.Telerik().ComboBox()
.Name("ComboBox")
.BindTo(new SelectList((IEnumerable<Person>)ViewData["people"], "Id", "Name"))
.ClientEvents(events => events.OnChange("ComboBox_onChange")
.OnLoad("ComboBox_onLoad")
.OnOpen("ComboBox_OnOpen"))
.Render();
%>
I can get the text of a specific item using:
var item = combobox.dropDown.$items.eq(0);
alert(item.text());
Any idea how I can get the data of this item to check if the value of viewed if it is true of false.
I tried:
alert(item.Value.split('|'));
but got this error: Unable to get value of the property 'split': object is null or undefined
Thanks in advance.
Here is the Client API documentation
Basically you need to get a reference to the combobox plugin first:
var combo = $('#ComboBox').data('tComboBox');
Then you can call whatever method you need:
alert( combo.value() );
EDIT:
Your combobox is currently bound to a SelectList where the Value is Person.Id and Text is Person.Name. The "viewed" property is not stored anywhere. You could include it as part of the Value, retrieve the value as I described above, and then split() to get the "viewed" part:
Html.Telerik().ComboBox()
.Items(items =>
{
// pseudo-code
foreach (person)
items.Add().Text(person.Name).Value(person.Id + "_" + person.Viewed)
})
...