1
votes

I am supposed to create a functionality where the MVC.NET Core application submits the JSON request through a Web API within the public async Task OnPostAsync(Request request) method

public async Task<IActionResult> OnPostAsync(Request request)
{    
   if(request.OwnershipActivity == null || request.OwnershipActivity.Ownership == null)
    {
        ModelState.AddModelError("OwnershipAnswer", "You must select an option.");               
    }
    if (!ModelState.IsValid)
    {                
        return Page();
    }
    //Do POST to database then redirect to Index
    //Submit JSON
    creturn RedirectToPage("../Index");
}

How do I convert a current Model(Form) into a JSON?

Thank you for your help

2
Newtonsoft.Json serializerMorten Bork
This look slike a razor pages question as opposed to a standard MVC question, you need to clarify.Nkosi
Nkosi, thank you, I updated the tags, Morten, you hint solved my probelmJames

2 Answers

2
votes

It turned out to be beyond simple

string requestJson = JsonConvert.SerializeObject(request);
1
votes

Try use this

public async Task<IActionResult> OnPostAsync(Request request)
{    
    ...
    return Json(yourObject);
}

See the link of the documentation