0

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);
            }
        }
    }
9
  • 1
    Given that Visual Studio is an IDE I don't see what relevance it has to your issue? You can use Notepad if you like and the code would still work. Is the issue perhaps that you've now split the frontend and backend logic in to multiple projects and you're only running one of them at a time? Commented Jan 17, 2022 at 10:20
  • 1
    You can create partialview for that code and you can simply return partialview and you will get html as a response in js. Commented Jan 17, 2022 at 12:08
  • @KiranJoshi return partialview from controller ? controller is not part of UI layer it is part of Web API layer and it returns only the data not the partial view. In this case how can I do it ? Commented Jan 17, 2022 at 14:57
  • Just to put it simple, onclick of left menu I do not want to reload the entire screen. Only the content section I want to load it based on the WEB Api data Commented Jan 17, 2022 at 15:00
  • Just noticed, even in stack overflow, on click of the left menu, it loads the entire page. Just curious why is it done this way? Menus, Top Bar and the footer need not have to reload right? Am I thinking wrong ? Commented Jan 17, 2022 at 15:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.