Hello everyone I have call this function by ajax call
public JsonResult OnGetUserDetails(string User)
{
var currentDate = DateTime.UtcNow.Date;
var usersDetails = _con.Users
.Join(
_con.Claims.Where(c => c.Type == "Status" && c.Value == "active"),
u => u.Id,
c1 => c1.UserId,
(u, c1) => new { User = u, Claim1 = c1 }
)
.Join(
_con.Claims.Where(c => c.Type == "End"),
uc1 => uc1.User.Id,
c2 => c2.UserId,
(uc1, c2) => new { Claim = uc1, Claim2 = c2 }
)
.AsEnumerable()
.Where(ucc => DateTime.Parse(ucc.Claim2.ClaimValue).Date > currentDate)
.Select(ucc => new UserDetailsViewModel
{
UserEmail = ucc.Claim.User.Email,
Name = ucc.Claim.User.FirstName
}).ToList();
return new JsonResult(usersDetails);
}
It's get successfully data from data base But I have faced a issue , how to return this razor view pages with foreach loop.
This is view code
@model List
| User Email | Name |
|---|---|
| @userDetails.UserEmail | @userDetails.Name |
Vishal JoshiPosted May 18, 2023, 12:05 PM
Hello Sunny
You just need to use Model to get the data in razor view. please check replace below view code with your code.
Thanks
Mohamed Azarudeen ZPosted May 18, 2023, 7:56 AM
Yes it is possible sunny.
Sourabh DhimanPosted May 18, 2023, 7:53 AM
Mohamed Azarudeen -- Please let me know It is without PartialView possible or not.
Mohamed Azarudeen ZPosted May 18, 2023, 7:43 AM
To return the razor view page with the fetched data from the database using an AJAX call, you can make use of the `PartialView` method in your `JsonResult` action. Here's an updated version of your code:
1. Modify the `JsonResult` action method to return a partial view instead of a JSON result:
```csharp
public IActionResult OnGetUserDetails(string User)
{
var currentDate = DateTime.UtcNow.Date;
var usersDetails = _con.Users
.Join(
_con.Claims.Where(c => c.Type == "Status" && c.Value == "active"),
u => u.Id,
c1 => c1.UserId,
(u, c1) => new { User = u, Claim1 = c1 }
)
.Join(
_con.Claims.Where(c => c.Type == "End"),
uc1 => uc1.User.Id,
c2 => c2.UserId,
(uc1, c2) => new { Claim = uc1, Claim2 = c2 }
)
.AsEnumerable()
.Where(ucc => DateTime.Parse(ucc.Claim2.ClaimValue).Date > currentDate)
.Select(ucc => new UserDetailsViewModel
{
UserEmail = ucc.Claim.User.Email,
Name = ucc.Claim.User.FirstName
}).ToList();
return PartialView("_UserListPartial", usersDetails);
}
```
2. Create a partial view named "_UserListPartial.cshtml" (or any name you prefer) and place it in the appropriate folder. This partial view will render the table rows for the user details:
```html
@model List
@foreach (var userDetails in Model)
{
}
```
3. Make sure your main view includes the necessary JavaScript code to perform the AJAX call and update the HTML with the returned partial view. You can use the jQuery library for this purpose. Here's an example:
```html
```
Make sure to replace `'YourController'` with the actual name of your controller that contains the `OnGetUserDetails` action.
With these changes, when the page loads, the AJAX call will be made to the `OnGetUserDetails` action, and the returned partial view will be inserted into the `userListContainer` div on the page. The table rows with user details will be rendered based on the data returned from the server.