1

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" />
 }
Alma
  • 3,780
  • 11
  • 42
  • 78

1 Answers1

2

There's no redirect necessary, just return the view you want to display in the post controller action.

[HttpGet]
public ActionResult SearchUser()
{           
    return View();
}

[HttpPost]
public async Task<ActionResult> SearchUser(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 View("Create", userObject);
    }
    return View("Create", null);   // TODO go somewhere else if null
}

You view would look something like

@model SomeModel
@{
    ViewBag.Title = "Create";
}
@using (Html.BeginForm("create", "CreateLogin", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.TextBoxFor(model => model.Email, new { placeholder = "Email" })
    @Html.TextBoxFor(model => model.Phone, new { placeholder = "Phone" })
    ...
    <input id="btnSubmit" name="btnSubmit" type="submit" value="Create" />
}

but in order to do that you need to stop deserializing to an anonymous object and instead make a proper model class to deserialize to.

public class SomeModel
{
    public string Email { get; set; }
    public string Phone { get; set; }
    ...
}

Deserializing a JSON file with JavaScriptSerializer()

Community
  • 1
  • 1
Paul Abbott
  • 7,065
  • 3
  • 27
  • 45
  • Do you know how I can get access to values of userObject in Create() method(view). I nned to fill the value of HTML textboxes with the values from userobject. – Alma Apr 13 '16 at 23:00
  • the problem is I cannot use MVC controls I have to use regular HTML input. – Alma Apr 13 '16 at 23:16
  • Fine, then use ``. But `@Html.TextBoxFor` will produce exactly the same HTML, so why can't you use it? – Paul Abbott Apr 13 '16 at 23:20
  • they asked me to not use it as we are getting design from 3rd party that used HTML5 controls. – Alma Apr 13 '16 at 23:24