Can I update view before action had executed?

Stanislav Panferov 45 Reputation points
2023-05-01T18:23:26.6266667+00:00

My asp.net core 7 project has a Register view in which I need to disable some elements immediately after model validation before corresponding action completes, because it is long operation. I try to use View(model).ExecuteResultAsync(ControllerContext):

[HttpPost]
public async Task<IActionResult> Register(RegisterViewModel model)
{
    IActionResult actionResult = null;
    if(model.Submitted = ModelState.IsValid)
    {
        await View(model)?.ExecuteResultAsync(ControllerContext);
        //await PartialView("UserAccountSubmittedPartial")?.ExecuteResultAsync(ControllerContext);
        if(string.IsNullOrWhiteSpace(model.UserName))
        {
            model.UserName = UserManager.GenerateNewUserName();
        }
        BankApiUser newUser = new BankApiUser(model);
        IdentityResult identityResult = IdentityResult.Success;
        try
        {
            identityResult = await UserManager.CreateAsync(newUser, model.Password);
        }
        catch(Exception e)
        {
            IdentityResult.Failed(new IdentityError() { Description = e.Message });
            Debug.WriteLine(e);
            throw e;
        }
        if(identityResult?.Succeeded == true)
        {
            switch(model.LoginType)
            {
            case LoginType.LoginByUserName:
                actionResult = RedirectToAction("Login", newUser);
                break;
            case LoginType.LoginByEmail:
                await EmailHelper.SendConfirmationLinkMail(HttpContext, model);
                actionResult = View(model);
                break;
            case LoginType.LoginByPhoneNumber:
                await SmsHelper.SendVerificationCode(model);
                actionResult = View(model);
                break;
            }
        }
        else
        {
            actionResult = View(model);
        }
    }
    else
    {
        actionResult = View(model);
    }
    return actionResult;
}

In this case, scripts from the Registry view are not executed, and the contents of the view are displayed twice, one below the other. I've also tried running only those scripts that update the form. To do this, I created a partial view containing these scripts and executed the PartialViewResult as shown above in the comments. As result I receive HTTP error 500 after Register action execution.

How can I update view using my scripts before Register action completes?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,573 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,494 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,962 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 65,576 Reputation points
    2023-05-01T18:56:07.7033333+00:00

    web applications are a request / response. the browser requests (get) or submits (form post) and waits for the response html. when returned the browser pre-render the page.

    on the server the request is processed by an action, and the view builds the html that will be returned to the browser (razor pages coming the action and view into one component). neither the action nor the view can change the browser until the html is returned. so while server side code to build the response html may be async, it still has no effect on browser until the response is returned.

    to do what you want, you need to use client javascript to change the html after the submit and before the response is returned. typically you would attach to the form submit event, and make the desired html changes.

    you can get more control, by having client script post the form data via ajax. In this case, the javascript needs to process the response data itself and update the html with server changes.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.