2016-06-20 12 views
-1

Hallo die Ansicht kann nicht ausgelöst werden, aber es ist bereits seinen Controller getroffen. Ist es ein Deadlock? Weil ich GetAsync verwende, um die HttpResponse-Nachricht zu erhalten. Hier ist mein Code.MVC-Controller kann die Ansicht nicht auslösen

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

} 

-> Der Index kann die Ansicht nicht anzeigen.

+0

Sie geben 'bool' zurück, also wie eine Ansicht gezeigt wird? – Mairaj

+0

Dies ist nur eine Funktion, die innerhalb des Controllers –

+0

aufruft. Auch den Controller-Code anzeigen. – Mairaj

Antwort

0

Wenn Sie möchten, dass der Controller nach all Ihrem Prozess einen booleschen Wert zurückgibt, führen Sie diese Methode als Aktionsmethode (hier können wir Werte anzeigen).

[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

Sie müssen View zurückgeben. Jetzt kommen Sie zurück bool. ReturnType muss ActionResult sein

Verwandte Themen