2016-12-21 9 views
0

私のアプリケーションはMVC 5 C#です。私は、このAjaxのスクリプトを使用してコントローラから文字列値を取得しようとしています :ここJsonは別の文字列を返す

function testing() { 
     $.ajax({ 
      url: "../../Home/Test", 
      type: "GET", 
      data: { 
       id: 1 
      }, 
      success: function (result) { 
       if (result.PhotoURL) { 
        $("#Photo").html(result.PhotoURL); 
       } 
      }, 
      error: function (result) { 
       alert("error"); 
      } 
     }); 
    } 

がコントローラである:

public ActionResult Test(int id) 
     { 
      int updateid = 1; 
      string thephoto = "<img src=\"@Url.Action(\"TheImage\", 
      new {id = " + updateid + 
      "})\" class=\"mg-responsive\" style=\"margin-left: 
      auto;margin-right: auto\"/> "; 
      var result = new 
      { 
       PhotoURL = thephoto 
      }; 
      return Json(result, JsonRequestBehavior.AllowGet); 
     } 

私が取得:

<img class="mg-responsive" style="margin-right: auto; margin-left: auto;" 
src="@Url.Action(" {id='1})"' new="" thespillimage",=""> 
+0

あなたは何を得ることを望んでいましたか? – ADyson

+0

hncl

+1

これはあなたの 'Test()'メソッドが返すものです。 'string'に' @ Url.Action'のようなものを使用している点はありません。これはサーバーで評価され、スクリプトで評価されないカミソリコードです。実際のURLを返すだけです(メソッド '' url = Url.Action(...); 'を使用してURLを生成してください) –

答えて

0

は、私が戻って推薦します最初にupdateidを追加して、クライアント側でimgマークアップを構築することができます。

public ActionResult Test(int id) 
    { 
     int updateid = 1; 
     var result = new 
     { 
      PhotoId = updateid 
     }; 
     return Json(result, JsonRequestBehavior.AllowGet); 
    } 

そして、クライアント側の

function testing() { 
    $.ajax({ 
     url: "../../Home/Test", 
     type: "GET", 
     data: { 
      id: 1 
     }, 
     success: function (result) { 
      if (result.PhotoId) { 
       var PhotoUrl = "<img src='/YourController/TheImage/" + result.PhotoId + " class='mg-responsive' style='margin-left:   auto;margin-right: auto'/> "; 
       $("#Photo").html(PhotoURL); 
      } 
     }, 
     error: function (result) { 
      alert("error"); 
     } 
    }); 
} 

これは、帯域幅を節約し、それがきれいです。イメージのクラスまたはプロパティの属性を変更する場合は、ソリューションを再構築する必要はありません。

関連する問題