2016-04-13 64 views
1

我是MVC的新手,我創建了一個頁面,在文本框中獲取名字和姓氏,並運行api獲取客戶信息。代碼中的userObject包含Jason格式的用戶信息。當我從API獲取值時,我需要使用用戶信息文本框重定向到另一個頁面(視圖),並使用從API返回的信息填充該文本框。重定向到另一個視圖並傳遞json值

我不知道如何重定向到另一個視圖,以及如何攜帶Jason格式數據來填充文本框。 我正在寫1個控制器和2個視圖。

這是我的控制器代碼:

[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(); 
    } 

這是得到的第一個名字和姓氏的第一個觀點:

@{ 
    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"  /> 
    } 

這是我喜歡來填補這些第二種觀點文本框與我從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" /> 
} 

回答

2

沒有必要的重定向,只是返回您想要在後控制器操作中顯示的視圖。

[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 
} 

查看看起來像

@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" /> 
} 

但爲了做到這一點,你需要停止反序列化到一個匿名對象,而是作出適當的模型類反序列化到。

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

Deserializing a JSON file with JavaScriptSerializer()

+0

就是它了,謝謝你。 – Alma

+0

你知道我如何在Create()方法(視圖)中訪問userObject的值嗎?我想用來自userobject的值填充HTML文本框的值。 – Alma

+0

已更新回答... –

相關問題