Here is the code I used to do it in Visual Studio 2019
$.ajax({
type: "POST",
url: url,
data: { code: "ABC" },
success: function (response) {
$('.content-wrapper').html(response);
},
error: function (request, status, error) {
}
});
Here "response" is the partial view result. But now I an using visual studio 2022 and Controller is in different web api project. I am getting only the data from API and need to render the HTML in client application.
window.location.href = nextUrl;
I can call the above line, but this will load the whole page instance I want to load only the content-wrapper div without being loading the whole page.
EDIT If I reload the entire page then, need to store the menu which was expanded / clicked to retain the values. Instead it would be better to load only the content area which is on the right side. I was able to achieve this before making separate UI and Web API projects.
EDIT
On click of the left menu "fnMenuClick()" will get called and it will call the Web API "UpdateLastAccessedOn" If the Web API call is success need to render the content-wrapper div so that entire page is not getting reloaded
function fnMenuClick(item) {
var url = "Home/UpdateLastAccessedOn";
var actionCode = $(item).data('user-action');
var lastAccessedObj = { ActionCode: actionCode };
$.ajax({
type: "POST",
url: webServiceUrl + url,
data: JSON.stringify(lastAccessedObj),
contentType: "application/json; charset=utf-8",
traditional: true,
dataType: "json",
success: function (response) {
//Need write the logic to update the DIV which has "content-wrapper" which is in content area based on the "actionCode"
//$('.content-wrapper').load("Layout.cshtml");
//If the controller returns the partial view then, I could have simply use the below statement to load the html on to the div
//$('.content-wrapper').html(response);
},
error: function (response, status, error) {
}
});}
Web API Code
[HttpPost(Name = "UpdateLastAccessedOn")]
public IActionResult UpdateLastAccessedOn([FromHeader(Name = "Auth-Token")] string authToken, [FromBody] UserAction userAction)
{
_userSession = WebApiHelper.GetUserSession();
string message = $"Unauthorized - ";
string displayMessage = $"Unauthorized - Session Details not found";
if (_userSession == null)
throw new UnauthorizedException(message + "|" + displayMessage);
else
{
SaveEntity saveEntity = new()
{
PrimaryActionCode = userAction.ActionCode,
ActionStartedOn = DateTime.Now,
ActionPerformedBy = _userSession!.LoginId,
UserLoginAuditId = _userSession.UserLoginAuditId
};
bool flag = _tenantCacheService.InsertActionAudit(saveEntity);
if (flag)
{
flag = _userLoginAuditService.UpdateLastAccessedOn(_userSession.UserLoginAuditId);
logMessage($"User Last Accessed On Success for - {_userSession.LoginId}");
ResponseObject responseObject = new()
{
StatusCode = StatusCodes.Status200OK.ToString(),
Message = "User Last Accessed On Success",
InternalObject = new UserObject()
{
LoginId = _userSession.LoginId,
ActionCode = userAction.ActionCode
}
};
return Ok(responseObject);
}
else
{
logMessage($"User Last Accessed On Failed for - {_userSession.LoginId}");
throw new UnauthorizedException(message + "|" + displayMessage);
}
}
}