I have a simple .net MVC application that takes firstname and lastname from my view and submits POST request to web api that creates user and sends response back as HttpResponseMessage.
When I provide firstname and lastname and hits submit the api receives request in POST and does its logic and then it returns HttpResponseMessage with my response(in this case "Successfully created user account" text).Content with string and response.status. Now on the browser after I submit I move away from the view I was on and I see blank white page with response.Content string displayed.
What I want to do is to take the response and display it on the same view I was on. Any ideas on how to do this?
My controller
Controllers/HomeController.cs
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
Razor View content
Views/Home/Index.cshtml
<form name="createtestuserinput" action="api/createuser" method="POST">
FirstName
<input type="text" id="firstname" name="firstname">
Lastname
<input type="text" id="lastname" name="lastname">
<input type="submit" id="createAccount" value="Create Account" />
</form>
Web Api snippet
public class Createuser : ApiController
{
public HttpResponseMessage Post(HttpRequestMessage request)
{
var response = new HttpResponseMessage();
//some logic happens here
response.Content = new StringContent("Successfully created user account");
response.StatusCode = HttpStatusCode.OK;
return response;
}
}