I am new to MVC, I create a page that get the first name and last name in text boxes and run an api to get the customer info. The userObject in the code bellow contains the user information in Jason format. When I get the value back from API I need to redirect to another page(view) with user info text boxes and fill out that text boxes with the info I got back from API.
I am not sure how I can redirect to another View and how carry Jason format Data to fill the text boxes there. I am writing all in 1 controller and 2 views.
This is my controller code:
[HttpGet]
public ActionResult SearchUser()
{
return View();
}
[HttpPost]
public async Task<JsonResult> SearchUser2(UserSearchRequest userSearchRequest)
{
HttpClient client = new HttpClient();
object userObject = null;
string baseUrl = "http://test/api ";
if (userSearchRequest.LastName != null && userSearchRequest.Zip != null && userSearchRequest.Ssn !=null)
{
var response = await client.GetAsync(string.Format("{0}{1}/{2}/{3}", baseUrl, "/users", userSearchRequest.FirstName, userSearchRequest.LastName));
if (response.IsSuccessStatusCode)
{
userObject = new JavaScriptSerializer().DeserializeObject(response.Content.ReadAsStringAsync().Result) as object;
}
}
if (userObject != null)
{
return Json(new { user = userObject }, JsonRequestBehavior.AllowGet);
}
return Json(string.Empty);
}
//this is where I like to redirect to and fill the textboxes with user Info
[HttpPost]
public ActionResult Create()
{
return View();
}
This is the First view that get the first Name and Last Name:
@{
ViewBag.Title = "SearchUser";
}
@using (Html.BeginForm("SearchUser2", "SignUp", FormMethod.Post))
{
@Html.AntiForgeryToken()
<input id="FirstName" name="FirstName" type="text" placeholder="FirstName" />
<input id="LastName" name="LastName" type="text" placeholder="LAST NAME" />
<input id="btnSubmit" name="btnSubmit" type="submit" value="SIGN UP TODAY" />
}
And this is the second view that I like to fill these textboxes with the data that I get from API.
@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm("create", "CreateLogin", FormMethod.Post))
{
@Html.AntiForgeryToken()
<input id="Email" name="Email" type="text" placeholder="Email" />
<input id="Phone" name=" Phone " type="text" placeholder=" Phone " />
<input id="Address" name=" Address " type="text" placeholder=" Address " />
<input id="btnSubmit" name="btnSubmit" type="submit" value="Create" />
}