Telerik Forums
UI for ASP.NET Core Forum
1 answer
96 views

In the example below products is actually DbSet<Product>

public async Task<ActionResult> Products_Read([DataSourceRequest]DataSourceRequest request)
{
    using (var northwind = new SampleEntities())
    {
        IQueryable<Product> products = northwind.Products;
        DataSourceResult result = await products.ToDataSourceResultAsync(request);
        return Json(result);
    }
}

Under the hood the ToDataSourceResultAsync call is executed inside Task.Run

And that Task.Run is calling sync methods of IQueryable which means EntityframeworkCore queries are not taking place with async I/O

 

I came up with something like below. But couldnt figured out how to support groups and aggregates.

It works with filters, sorts and paging.


        public static Task<AsDataSourceResult<TResult>> ToAsDataSourceResult<TModel, TResult>(this IQueryable<TModel> enumerable, DataSourceRequest request, Func<TModel, TResult> selector)
        {
            return enumerable.AsCreateDataSourceResultAsync(request, selector);
        }

        private static async Task<AsDataSourceResult<TResult>> AsCreateDataSourceResultAsync<TModel, TResult>(this IQueryable<TModel> queryable, DataSourceRequest request, Func<TModel, TResult> selector)
        {
            var result = new AsDataSourceResult<TResult>();

            var data = queryable;

            if (request.Filters.Count > 0)
                data = (IQueryable<TModel>)data.Where(request.Filters);

            result.Total = await data.CountAsync();

            var sort = new List<SortDescriptor>();

            if (request.Sorts != null)
                sort.AddRange(request.Sorts);

            if (sort.Count == 0 && queryable.Provider.IsEntityFrameworkProvider())
            {
                // The Entity Framework provider demands OrderBy before calling Skip.
                var sortDescriptor = new SortDescriptor
                {
                    Member = queryable.ElementType.FirstSortableProperty()
                };
                sort.Add(sortDescriptor);
            }

            if (sort.Count > 0)
                data = (IQueryable<TModel>)data.Sort(sort);

            data = data.Page(request.Page - 1, request.PageSize);

            var list = new List<TResult>();

a

            result.Data = list;

            return result;
        }

        private static IQueryable<TResult> Page<TResult>(this IQueryable<TResult> source, int pageIndex, int pageSize)
        {
            var query = source;

            query = query.Skip(pageIndex * pageSize);

            if (pageSize > 0)
                query = query.Take(pageSize);

            return query;
        }

        private static string FirstSortableProperty(this Type type)
        {
            var firstSortableProperty = type.GetProperties().FirstOrDefault(property => property.PropertyType.IsPredefinedType()) ?? throw new NotSupportedException("Exceptions.CannotFindPropertyToSortBy");
            return firstSortableProperty.Name;
        }

        private static bool IsEntityFrameworkProvider(this IQueryProvider provider)
        {
            var name = provider.GetType().FullName;
            return name == "System.Data.Objects.ELinq.ObjectQueryProvider" ||
                    name == "System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider" ||
                    name.StartsWith("LinqKit.ExpandableQueryProvider") ||
                    name.StartsWith("Microsoft.Data.Entity.Query.EntityQueryProvider") ||
                    name.StartsWith("System.Data.Entity.Internal.Linq");
        }

 

AsDataSourceResult<TResult> is wrapper for typed access to DataSourceResult

How do i support all operations which ToDataSourceResult (sync) one supports.

Mihaela
Telerik team
 answered on 21 Oct 2024
1 answer
60 views

I have list of tabs in my model like

var model = new List<TabModel>(){
new TabModel(){
Name = "Users",
ControllerName = "Home",
ActionName = "SearchUserData",
RequestValues = new {
area = "Admin",
userId = 12
}
},
new TabModel(){
Name = "Location",
ControllerName = "Home",
ActionName = "SearchUserLocation",
RequestValues = new {
area = "Admin",
userId = 12
}
},
new TabModel(){
Name = "Cities",
ControllerName = "Home",
ActionName = "SearchUserCity",
RequestValues = new {
area = "Admin",
userId = 12,
countryId = 1
}
},
};


In cshtml: my Tabstrip looks like this

Html.Kendo().TabStrip()
    .Name("admin-tab-strip")
    .Items(s =>
    {

        foreach (var item in Model)
        {
            if (item.ControllerName == "SearchUserData")
            {
                s.Add().Text(item.Name)
                    .Content(@Html.Action(item.ActionName, item.ControllerName, item.RequestValues).ToString())
                    .Selected(item.IsSelected);
            }
            else
            {
                s.Add().Text(item.Name)
                    .LoadContentFrom(item.ActionName, item.ControllerName, item.RequestValues)
                    .Selected(item.IsSelected);
            }
        }
    }))


Since Html.Action is no longer works in .net 8. Need suggestion to overcome this issue.
Now, I'm unable to navigate to the page which has this tabstrip due to Html.Action issue

Ivan Danchev
Telerik team
 answered on 18 Oct 2024
1 answer
38 views
I have a notification element that I want to position with some margin on the right side of the screen on desktop, so it doesn’t sit too close to the edge. Using .Position(p => p.Right(80)) achieves this, but on mobile, the notification overflows off the left side of the screen. Is there a way to conditionally apply the right positioning based on screen width?

Additionally, on mobile, I’d like to set the width to something like 80% of the screen and center the notification. On desktop, I want it to maintain its default width. Is this possible?

P.S. This is for Telerik ASPNet Core UI.
Alexander
Telerik team
 answered on 17 Oct 2024
1 answer
62 views

I have a grid with the following template columns:

columns.Template("<input type='checkbox' />");

columns.Template("<input type='hidden' value='#=Ssid#' />");

Html.Kendo().Template().AddHtml() cannot be used because it does not return a string.

How can I rewrite the code to be compatible with a strict content security policy (e.g. without using unsafe-inline)?

Thank you

Eyup
Telerik team
 answered on 13 Oct 2024
1 answer
219 views
I have a dropdownListFor in a View which is shared within the tabs in the Kendo Tabstrip. Due to cannot override component name when bound to a model expression error I have commented the Name configuration

My Dropdown:
@(Html.Kendo().DropDownListFor(m => m.Country)
       //.Name("city-" + (int)Model.CityID) -- Commented to resolve the error
       .DataSource(source =>
       {
           source.Read(read =>
           {
               read.Action("GetCities", "Home", new { countryID = (int)Model.CountryID})Type(HttpVerbs.Post);
           });
       })
       .OptionLabel("(Please Select))
       .HtmlAttributes(new
       {
           data_bind = "value: cityValue, events: { change: selectedCityChanged }"
       })
)

I have tab strip in which i don't have any issue with the first tab, but for other tabs, the control is not binded properly. Earlier i had the Name to prevent this bind issue.. Need help to fix this issue

I'm using Kendo 2024.3.806 version and ASP.NET Core MVC 8

First Tab:


Second Tab:
Renu
Top achievements
Rank 1
Iron
Iron
Iron
 answered on 12 Oct 2024
0 answers
31 views

Grouping colapsed after the save.

Grouping should be in the same state as before the save 

 

Need help on this issue. will waiting for you response.

Gaurav
Top achievements
Rank 1
 asked on 10 Oct 2024
2 answers
76 views

We have a complicated Kendo Grid with Tag Helper notification. We need to use On Cancel Event within edit operation, by pressing Cancel button. Here is an example (but not Tag Helper)

@(Html.Kendo().Grid<MyModel>()
    .Name("grid")
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .Events(events => events.Cancel("onCancel")) // JavaScript event handler
    .DataSource(dataSource => dataSource
        .Ajax()
        .Model(model => model.Id(m => m.Id))
        .Read(read => read.Action("Read", "Controller"))
        .Update(update => update.Action("Update", "Controller"))
    )
    .Columns(columns =>
    {
        columns.Bound(c => c.Id).Editable(false);
        columns.Bound(c => c.Name);
        columns.Command(command => command.Edit());
    })
)

<script>
    function onCancel(e) {
        console.log("Edit operation was canceled.");
        // e.container is the jQuery object of the edited row or cell.
        // You can use this function to handle custom logic after canceling
    }
</script>
Jens
Top achievements
Rank 1
Iron
 answered on 10 Oct 2024
1 answer
95 views

I have an Editor on a page and there are several Content Security Policy violations in the console. Here is an example:

Refused to apply inline style because it violates the following Content Security Policy directive: "style-src-attr 'unsafe-hashes' 'sha256-aqNNdDLnnrDOnTNdkJpYlAxKVJtLt9CtFLklmInuUAE='". Either the 'unsafe-inline' keyword, a hash ('sha256-5TmCSWsRHHKtNC4AgS23KS5Z9SBqma0xikI6H6iJ1/Y='), or a nonce ('nonce-...') is required to enable inline execution.

Is it possible to use an Editor without adding unsafe-inline to style-src-attr?

Thank you

Ivan Danchev
Telerik team
 answered on 09 Oct 2024
1 answer
53 views
We are enabling strict CSP policies. We have managed to get all components working with the exception of the editor component.  The default was to set the height of the component is to set a style='height:400px; width:300px" This causes the browser to complain about in-line styles. Is there another way to set the height and width of the component? We are getting issues with the setting of in-line styles by the editor. We are currently setting the nonce property on the tag helper for the editor component. Are we missing something?
Mihaela
Telerik team
 answered on 09 Oct 2024
1 answer
46 views

Hello

We are currently working on improving the content security policy header of our application to remove the unsafe-inline and unsafe-eval for the script element.

I am facing some issues when using the Template element with the generated deferred javascript mostly in the grid and I hope someone could help me with that. 

Firstly, I'd like to know if there are some tag helpers that could be used to generate some template. To be more explicit : we use some shared and partial views to render the components in edition mode in our grids. These views are written using tag helpers, for instance we have the following file Views\Shared\EditorTemplates\DateTime.cshtml

@model DateTime

 

<kendo-datetimepicker asp-for="@Model" />

Used in our grids :

column.Bound(User => User.DateCreated).EditorTemplateName("DateTime")

(this is a basic example but we have some more complex views using home made components with taghelper syntax)

I noticed there is a kendo-template tag helper and I would like to know if there is any way to use it in our views, so I could replace my view with the following code and use the EditorTemplateComponentName extension method in my grid :

<kendo-template>
    <kendo-datetimepicker asp-for="@Model" />
</kendo-template>

When I tried to do this I have an exception :

 

Secondly, I noticed an issue when using some kendo elements in editor templates: the javascript rendered in the deferred script file includes the initialisation scripts at the root of the script file. This mainly cause some issues in my pages when some other element has the same names (but I think it can lead to some other issues).

For instance, I have the following column definition in my grid:

<columns>
    <column field="FullName" title="FullName"></column>
    <column field="DateCreated" title="DateCreated">
        <column-editor-template>
            <kendo-datetimepicker name="dateCreated"></kendo-datetimepicker>
        </column-editor-template>
    </column>
</columns>

Then in the kendo-deferred-script javascript file I can see the dateCreated element is inialized at the beginning of the file and in the template function:

 

I'm joining a sample application with both cases to help reproduce the issues.

Is there something I'm doing wrong or any improvement coming in the next versions to fix these issues ?

 

Thanks in advance

Yael

Ivaylo
Telerik team
 answered on 30 Sep 2024
Narrow your results
Selected tags
Tags
+? more
Top users last month
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Jianxian
Top achievements
Rank 1
Iron
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Jim
Top achievements
Rank 2
Iron
Iron
Nurik
Top achievements
Rank 2
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Jianxian
Top achievements
Rank 1
Iron
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Jim
Top achievements
Rank 2
Iron
Iron
Nurik
Top achievements
Rank 2
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?