2017-01-23 4 views
0

JSONデータを使用してMVC RazorビューページのViewBagを更新しようとしています。しかし、それはViewBagまたはDivを更新していません。私は成功の警告を見て、それは更新された値を表示していますが、ページ内で、値を変更していません。最初にページがロードされるときに、値が表示されます。JSONデータを使用してMVC Razorビューページのデータを更新します。JQuery

私のjQueryのコードがある -

$(document).ready(function() 
    { 
     $('#ddl2').change(function (e) 
     { 
      e.preventDefault(); 
      var str_test = $("#v_ddl2 :selected").text();   
      var data1 = {str_test: str_test }; 
      var url = '@Url.Action("ddl_2")?id=' + id; 

      $.post(url, data1, function (data) 
      { 
       $('#nav_div').html(data); 
       alert(JSON.stringify(data)); // ALERT showing the updated data 
      }); 

     }); 
    }); 

コントローラ -

public JsonResult ddl_2(string str_test) 
    { 
     GetDat(str_test); 

     return Json(new { success = true, ViewBag.str_display }); 
    } 

HTML -

<div id='#nav_div'>@ViewBag.str_display</div> 

誰かがViewBagまたは本部が更新されたデータが表示されますどのように私を助けてもらえます。

おかげ

+0

+0

にあなたのHTMLを変更する、あなたでなければなりませんGetDat()メソッド内でstr_displayの値を設定します。なぜGetDat()から値を返して代わりに使用しないのですか? ViewBagはView側で使用し、サーバー側で値を渡さない方が良い –

+0

@KD、ありがとう、私はあなたの助言に従おうとします – jacklondon

答えて

0

次の試みることが含まれています。

public JsonResult ddl_2(string str_test) 
{ 
     GetDat(str_test); 

     return Json(new { success = true, display = ViewBag.str_display }); 
} 

を次のようにreturn文を変更し、

$.post(url, data1, function (data) 
{ 
     $('#nav_div').html(data.display); 
     alert(JSON.stringify(data.display)); // ALERT showing the updated data 
}); 

を次のようにあなたのjavascript(jqueryの)を変更し、ちょうど良い習慣を追加する<div id='nav_div'>@ViewBag.str_display</div>

+0

こんにちはAhamed、それは働いていますが、古い値を維持して、私は古い値を表示するよりも古い値を表示することを意味します – jacklondon

+0

あなたは、 div? –

+0

いいえ、常に更新された値 – jacklondon

0

あなたのIDは '#' 文字

<div id='nav_div'>@ViewBag.str_display</div> 
関連する問題