Home > Blockchain >  assign HttpPost async to view ASP .NET Core6 MVC
assign HttpPost async to view ASP .NET Core6 MVC

Time:01-19

pls help me to get it solved. I am making a ASP .NET Core 6 MVC Application where I have to make two screens login and after validate login show profile details via WEB API call. login is successful after that I am trying to use HTTPPOST to show details into view named home/Account . here is my code in home controller :-

[HttpPost]
        public async Task<IActionResult> Account(ReceivedToken token)
        {

            var companyForCreation = new ReceivedToken
            {
                email = "myemailid"
            };
            GetCompanyDetailsViewModel details = new GetCompanyDetailsViewModel();
            using (var httpClient = new HttpClient())
            {
                StringContent content = new StringContent(JsonConvert.SerializeObject(companyForCreation), Encoding.UTF8, "application/json");

                using (var response = await httpClient.PostAsync("webAPIURL", content))
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();
                        details = JsonConvert.DeserializeObject<GetCompanyDetailsViewModel>(apiResponse);
                    }
                    else
                    {
                        //ViewBag.StatusCode = response.StatusCode;
                        TempData["msg"] = response.StatusCode;
                    }
                }
            }
            return View(details);

        }

how can i Assign this HTTPPOST method directly to my view

@model GetCompanyDetailsViewModel
@{
    ViewBag.Title = "Account Page";
}
<html>
    <head>
        <style type="text/css">
        header{display:none;}

        body {
            background-color: darkseagreen;
        }
        </style>
   
    </head>
</html>
@if (Model != null)
{
   
    <h2>Welcome to the web Application</h2>

    @Model.bankAccount

    @Model.bankName

    @Model.email

}

CodePudding user response:

Your view does not belong to your POST method. It belongs to GET method called Account(). So after your POST method finishes its work send data to GET method with RedirectToAction(nameOf(Account))

  • Related