2016-06-20 105 views
-1

嗨視圖無法觸發,但它已經擊中了它的控制器。 這是一個僵局嗎?因爲我使用GetAsync來獲取HttpResponse消息。 這是我的代碼。MVC控制器無法觸發視圖

private bool GetCoordinates(string address) 
    { 
     bool result = false; 

     HttpClient client = new HttpClient(); 
     client.BaseAddress = new Uri(HttpUtility.UrlPathEncode("http://locationInfo/GetLocation?address=" + address)); 

     client.DefaultRequestHeaders.Accept.Add(
     new MediaTypeWithQualityHeaderValue("application/json")); 

     HttpResponseMessage response = client.GetAsync("").Result; 
     if (response.IsSuccessStatusCode) 
     { 
      JObject objs = JObject.Parse(response.Content.ReadAsStringAsync().Result); 
      int count = 0; 
      string[] cor = new string[2]; 
      foreach (var item in objs) 
      { 
       if(item.Key.ToString() == "location") { 
        foreach (var it in item.Value) 
        { 
         if (count <= 1) 
         { 
          cor[count] = it.ToList()[0].ToString(); 
         } 
         count++; 
        } 
       } 
      } 

      Xcor = cor[0]; 
      Ycor = cor[1]; 
      result = response.IsSuccessStatusCode; 
     } 

     response.Dispose(); 
     client.Dispose(); 
     return result; 
    } 

} 

- >索引無法顯示視圖。

+0

您正在返回'布爾',以便如何顯示視圖? – Mairaj

+0

這只是一個在控制器內部調用的函數 –

+0

也顯示控制器代碼。 – Mairaj

回答

0

如果您希望控制器在您的所有進程完成後返回布爾值,請將此方法設爲Action Method(此處我們可以返回值查看)

[HttpPost] 
public ActionResult Index(string address) 
{  
    ModelClass model = new ModelClass(); 
    model.yourModelVal = GetCoordinates(address); 
    return View(model); 
} 

public bool GetCoordinates(string address) 
{ 
    bool result = false; 

    HttpClient client = new HttpClient(); 
    client.BaseAddress = new Uri(HttpUtility.UrlPathEncode("http://locationInfo/GetLocation?address=" + address)); 

    client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json")); 

    HttpResponseMessage response = client.GetAsync("").Result; 
    if (response.IsSuccessStatusCode) 
    { 
     JObject objs = JObject.Parse(response.Content.ReadAsStringAsync().Result); 
     int count = 0; 
     string[] cor = new string[2]; 
     foreach (var item in objs) 
     { 
      if(item.Key.ToString() == "location") { 
       foreach (var it in item.Value) 
       { 
        if (count <= 1) 
        { 
         cor[count] = it.ToList()[0].ToString(); 
        } 
        count++; 
       } 
      } 
     } 

     Xcor = cor[0]; 
     Ycor = cor[1]; 
     result = response.IsSuccessStatusCode; 
    } 

    response.Dispose(); 
    client.Dispose(); 
    return result; 
} 
0

您需要回到View。現在你正在返回boolReturnType必須是ActionResult

相關問題