Telerik Mvc Grid and sub-property like Contact.Name when the contact is null - nullreferenceexception

i use telerik grid in an mvc project.
I have a 'complex' model that i don't want change and have a structure like this:
task
task.Contact
task.Contact.FirstName
in telerik grid i want to show all the task with the name of the Contact...but the contact CAN be null: in this case telerik return (rightly) an error for nullreferenceException, how can avoid this and display an empty value in the column?
columns.Bound(p => p.Contact.FullName).Title("Contact").Width(250);
tnx at all

You can do this by specifying the Template of the bound column:
columns.Bound(p => p.Contact.FullName)
.Template(p =>
{
%>
<%= (p.Contact != null ? p.Contact.FullName : "") %>
<%
}
.Title("Contact")
.Width(250);
Or you can use ClientTemplate:
columns.Bound(p => p.Contact.FullName)
.ClientTemplate("<#= Contact? Contact.FullName : '' #>");

Related

kendo mvc ajax grid sending Viewbag or ViewData variable as a parameter in client Template Column

I'm using Telerik Kendo MVC Grid ,and i need to use client template as a link.
The Question is : How to send ViewBag Variable or ViewData as a parameter within the link?
Just the syntax for creating a ClientTemplate using a ViewBag field?
#{
ViewBag.Data = "kendo-mvc-ajax-grid-sending-viewbag-or-viewdata-variable-as-a-parameter-in-clien";
}
#(Html.Kendo().Grid<GridViewModel>()
.Name("grid")
.Columns(cols =>
{
cols.Bound(c => c.Field1);
cols.Bound(c => c.Field2);
cols.Bound(c => c.Field2);
.ClientTemplate(
"<a href='http://stackoverflow.com/questions/40106180/" + ViewBag.Data + "' target='_blank'>Click Here</a>" );
})
}
...rest of grid config....
)
Otherwise, you need to provide more specific details of what you are asking.

Telerik grid shows an empty column after delete command

I am using Telerik MVC Extensions Grid control, with AJAX binding, and run a delete command. Deletion works as intended, and the grid updates so that it doesn't show the deleted row.
However, after deleting one of the grid columns (the first one) shows up empty. There is also difference in the second column - 'false' instead of the unchecked box.
Any ideas why, and how do I fix that?
I can refresh the screen, and that fixes the view. But it is a heavy page, and I'd rather not do the second refresh.
After deleting, the first column shows up empty:
My grid:
Html.Telerik()
.Grid(Model)
.Name("scenarioGrid")
.DataBinding(dataBinding => dataBinding.Ajax()
.Delete("Delete", "Scenario"))
.DataKeys(keys => keys.Add(c => c.Id))
.Columns(columns =>
{
columns.Template(o => Html.ActionLink(o.Name, "Index", new {id = o.Name})).Title("Scenario")
.FooterTemplate(#<text>Total #Model.Count() </text>);
columns.Bound(o => o.IsLocked);
columns.Bound(o => o.ContractMonth);
columns.Bound(o => o.CreateDate);
columns.Command(commands => commands.Delete().ButtonType(GridButtonType.Image)).Title("Delete");
}
)
.Sortable()
.Scrollable(scroll => scroll.Height(200))
.ClientEvents(events => events.OnDelete("onDelete").OnComplete("afterDelete"))
You shouldn't use columns.Template with Ajax binding. It's meant for server side bound grids. You should use
columns.Bound(o => 0.whatever).ClientTemplate("convert your link to a string here");
It looks like you are using server binding to load the grid, but ajax binding to update it. columns.Template is used for server binding. You should use ClientTemplate for ajax binding.

Telerik MVC Grid: Row specific content in a HtmlAttributes

Is it posible to use row specific content into HtmlAttributes?
I got this cell with its content (o.ArrivalTime), when i move my mouse over it i'll like it to show the content from a other element (o.Note) in a tooltip
I tried this but it will not accept the o.Note
columns.Bound(o => o.ArrivalTime)
.Title("Arrival Time")
.Template(o =>
{%><%=(o.ArrivalTime < Convert.ToDateTime("2000-01-01")) ? "?" : o.ArrivalTime.ToString()%><%})
.Width(140)
.HtmlAttributes(new {title = o.Note })
;
Rather than using HtmlAttributes, you can do this inside Template.
columns.Bound(o => o.ArrivalTime)
.Title("Arrival Time")
.Template(o =>
{%><div title="<%= o.Note %>"><%=(o.ArrivalTime < Convert.ToDateTime("2000-01-01")) ? "?" : o.ArrivalTime.ToString()%></div><%})
.Width(140)
;
Please take a look at the following example.
Grid - Server Templates
In this example the first column uses a templating mechanism to create the column. In similar way you can create a template for your column and then use the different columns while defining the template. Here is the code snippet from the demo:
<% Html.Telerik().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
//Template column. The grid displays the HTML defined by the argument.
columns.Template(c => {
%>
<img
alt="<%= c.CustomerID %>"
src="<%= Url.Content("~/Content/Grid/Customers/" + c.CustomerID + ".jpg") %>"
/>
<%
});
//Regular databound column. The grid displays the value of the CustomerID property.
columns.Bound(c => c.CustomerID);
})
.Render();
%>
Hope this was helpful to your question.
Lohith (Tech Evangelist, Telerik India)

cascading dropdownlist as bound column on telerik grid

I am not sure if anyone has ever come across an issue like this.
In my ASP.NET MVC application, I have a Telerik Grid control, which has the first 2 columns as dropdownlists. I have the editor template for each of these columns as telerik dropdownlists. These dropdownlists are in user control (.ascx) files. The code for the ascx files is below:
User Control 1:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%=Html.Telerik().DropDownListFor(m => m)
.BindTo(new SelectList((IEnumerable)ViewData["AccountTypeSelectList"], "lookUpCode", "description"))
%>
User Control 2:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%=Html.Telerik().DropDownListFor(m => m)
.BindTo(new SelectList((IEnumerable)ViewData["CreditAgenciesSelectList"], "description", "description"))
%>
The following is the code for my View where the Grid bound columns are:
#(Html.Telerik().Grid<DealerOfferBaseKPI>()
.Name("T_KPI_CA")
.DataKeys(key => key.Add(o => o.DealerOfferRuleDetailId))
.ToolBar(commands =>
{
commands.Insert().ButtonType(GridButtonType.ImageAndText).ImageHtmlAttributes(new { style = "margin-left:0" });
})
.Columns(columns =>
{
columns.Bound(o => o.AccountType).Title("Account Type").ClientTemplate("<#= AccountType #>").EditorTemplateName("AccountType");
columns.Bound(o => o.CreditAgency).Title("Credit Agency").ClientTemplate("<#= CreditAgency #>").EditorTemplateName("CreditAgency");
columns.Bound(o => o.PercentageAllowed).Title("Percentage Allowed");
columns.Bound(o => o.EffectiveDate).Title("Effective Date").EditorTemplateName("Date").Format("{0:MM/dd/yyyy}");
columns.Bound(o => o.ExpireDate).Title("Expire Date").EditorTemplateName("Date").Format("{0:MM/dd/yyyy}");
columns.Command(commands =>
{
commands.Delete().ButtonType(GridButtonType.BareImage);
}).Title("Actions");
})
.DataBinding(dataBinding =>
{
dataBinding.Ajax()
.Select("_SelectKPIBatchEditing", "DealerOfferManagement", new { filterType = "KPIcreditAgency" }).Enabled(true)
.Update("_SaveKPIBatchEditing", "DealerOfferManagement").Enabled(true);
})
.ClientEvents(ce => ce.OnSave("GridValidation"))
.Selectable()
.Scrollable()
.Pageable()
.Sortable()
)
I am trying to make these 2 dropdownlists as cascading. The values of the first dropdown are Residential, Commercial and Both. The values in the second dropdown are Equifax, Experian, TransUnion and Intelliscore. When I select residential in the first dropdown I want the second dropdown to show everything but not Intelliscore. For all other values of the first dropdown, I want all values of the second dropdown to show.
I am passing in the values of the 2 dropdowns by using 2 ViewData objects from my controller.
With the code shown, the values in the selectlist are displayed in the dropdowns just fine.
Any help is appreciated.
On the change clientevent of the first dropdown user javascript to add or remove element as explained here

How to bind Telerik MVC grid to DropDownList via Ajax?

I am trying to bind a dropdownlist to a telerik grid, so that when the value of the dropdownlist changes the grid will update via ajax to show results. Here is my code so far:
IndexView:
<% Html.RenderPartial("AptProfileFilter"); %>
<%= Html.Telerik().Grid(Model.profiles)//Initial Server Binding
.Name("Profiles").DataBinding(dataBinding => dataBinding
.Ajax()
.Update("_AjaxBinding", "AptProfile", new {id = (string)ViewData["BuildingID"]}))
.Columns(columns =>
{
columns.Bound(p => p.AptProfileID).Width(100);
columns.Bound(p => p.Apartment.Building.Complex.Name).Width(100);
columns.Bound(p => p.Apartment.Building.BuildingID).Width(100);
columns.Bound(p => p.Apartment.AptRate).Width(100);
})
.Pageable()
.Sortable()
I used FireBug to determine that the correct data is being posted( after selecting a element from my dropdownlist)
back to the Data collection in the GridModel class, but I do not understand why it is not updating the Grid with this new data?
I am very new to web development. Thanks for your help!
I've been wrestling with this too. take a look at the link below (bottom of comment), in which I asked the same type of question. In my case, I managed to get this working, but still have some questions about HOW I got it working. Anyway, I hope this helps ... Also, in my case, I'm using Razor views, which you might not be, based upon you use of the <% operators ... Telerik MVC Grid: How to use DropDownList in a column?

Resources