Select theme:
Custom business logic is key to most applications - call a third party API, execute complex database query or perform some calculation and return its result.
In Blazor applications Radzen supports this scenario out of the box via the Invoke method action type.
Radzen generates a <page-name>.razor.cs
file for every page. You can add methods to that file which you can then execute.
Let's create a method which returns the sum of its arguments.
Sum
.server\Pages\Sum.razor.cs
file in your your code editor of choice.SumComponent
partial class.public int Sum(int x, int y)
{
return x + y;
}
Sum
method from the dropdown. Radzen will load all methods of the page that you can invoke. If you don't see your method yet - click the reload button next to the dropdown.x
and y
parameters of the Sum
method.
Then
event of the invoke.${result}
.${result}
mean? The result
keyword is a placeholder which means "the result of the current custom method". Then sum
is the property which our custom method happens to use to return its result.Now run the application to test the Sum
method.
Here is what happened:
Sum
method with parameters x=1
and y=2
.Now let's pass the user input to the custom method. If you need a refreshment on getting user input in Radzen check the Properties help article.
x
and y
. They will store the user input and be the arguments of the Sum
method.x
and the second to y
.x
parameter to ${x}
and the y
parameter to ${y}
.
This will use the values of the x
and y
properties as method arguments.
Run the application to see it in action.
The previous examples used the sample custom method which doesn't do anything particularly useful. Let's do something real-life instead such as making a database query and returning its result. As usual we will use the Northwind database.
Let's make the method return the number of orders that ship to a user specified country.
Orders
to your application.server\Pages\Orders.razor.cs
file with your code editor.Inject
attribute.[Inject]
NorthwindContext NorthwindContext { get; set; }
OrdersComponent
class. It will return the number of orders that ship to the specified country.public int OrdersByCountry(string country)
{
var orders = NorthwindContext.Orders.Where(o => o.ShipCountry == country);
return orders.Count();
}
The UI will be simple:
Follow these steps:
country
and data-bind the TextBox to that property.
country
parameter to ${country}
.
Orders: ${result}
Run the application to try it:
Another common task is to display the result of a custom DB query in a DataGrid. We will use again the Northwind database and will display the number of orders per ShipCity in a DataGrid component.
Orders.razor.cs
file.public class OrderByCity
{
public string ShipCity { get; set; }
public int Count { get; set; }
}
OrdersComponent
partial classpublic IEnumerable<OrderByCity> OrdersByCity()
{
var orders = NorthwindContext.Orders.GroupBy(o => o.ShipCity)
.Select(group => new OrderByCity() {
ShipCity = group.Key,
Count = group.Count()
});
return orders;
}
OrdersByCity
method so we can use it with the DataGrid.${result}
.
${ordersByCity}
.If you run the application you should see the following in your browser:
Add custom method to the partial class similar to previous chapter and invoke it when needed.
[Inject]
YourDbContext Context { get; set; }
public async Task<int> UspUpdateEmployeeHireInfos(int? someID, string someOtherParam)
{
SqlParameter[] @params =
{
new SqlParameter("@returnVal", SqlDbType.Int) {Direction = ParameterDirection.Output},
new SqlParameter("@someID", SqlDbType.Int) {Direction = ParameterDirection.Input, Value = someID},
new SqlParameter("@someOtherParam", SqlDbType.VarChar) {Direction = ParameterDirection.Input, Value = someOtherParam}
};
Context.Database.ExecuteSqlRaw("EXEC @returnVal=[YourSPSchema].[YourSP] @someID, @someOtherParam", @params);
int result = Convert.ToInt32(@params[0].Value);
return await Task.FromResult(result);
}
Radzen is free to use. You can also test the premium features for 15 days.
Download NowSelect theme: