2014-03-14 106 views
1

我viewBag有一個用戶ID我想訪問ViewBag在我的對話框訪問變量

<script> 
    $(function() { 
     $("#dialog").dialog({ 
      autoOpen: false, 
      modal: true, 
      width: 339, 
      overlay: { 
       backgroundColor: 'red', 
       opacity: 0.5 
      }, 
      show: { 
       effect: "blind", 
       duration: 500 
      }, 
      hide: { 
       effect: "explode", 
       duration: 700 
      } 
     }); 
    }); 

    function OpenDialog() 
    { 
     $('#dialog').dialog("open"); 
    } 
</script> 

<div id="open" onclick="OpenDialog();">Click To Open</div> 

    @ViewBag.UserID 
    <div id="dialog"> 
      <table> 
       <tr> 
        <td>User ID</td> 
        <td>@ViewBag.UserID</td> 
       </tr> 
       <tr> 
        <td>Comments</td> 
        <td>@Html.TextArea("CommentsTextBox")</td> 
       </tr> 
      </table> 
      <div class="alignCenter"> 
       <input type="submit" value="Add" /> 
       <input type="button" value="Cancel" /> 
      </div> 
    </div> 
+0

什麼是你的問題/問題? –

+0

當我打開JQuery對話框​​@ ViewBag.UserID顯示空/空值。但外部@ ViewBag.UserID顯示正確的用戶ID – Golda

+0

您的代碼似乎沒有任何異常其正確的問題必須是不同的 – Rahul

回答

2

你的代碼應該有工作,但可能是jQuery的對話框的div <div id="dialog">

你可以試試下面的辦法改變的DOM元素值。

將UserId保留在隱藏字段中。對於用戶ID

<input type="hidden" id="userId" value="@ViewBag.UserID" /> 

製作佔位內<div id="dialog">

<div id="dialog"> 
    <table> 
     <tr> 
      <td>User ID</td>  
      <td><span id="dialogUserId"></span></td> 
     </tr> 
    </table> 
</div> 

獲取用戶名和上OpenDialog()事件分配形式佔位符。

<script> 
function OpenDialog() 
    { 
     $("#dialogUserId").val($("#userId").val()); //if this won't work try adding this line after dialog("open") 
     $('#dialog').dialog("open"); 
    } 
</script> 
0

做一件事把你的用戶ID普通的Java腳本變量和文件準備拿到的地方值到用j查詢

+0

你能舉一些例子嗎? – Golda

1

如果你只是想訪問內部jQuery的模型變量或ViewBag,你可以試試下面的方法。

<script type="text/javascript"> 

    function set(value) { 
     return value; 
    } 

    alert(set(@Html.Raw(Json.Encode(Model.Message)))); // Message set from controller 
    alert(set(@Html.Raw(Json.Encode(ViewBag.UrMessage)))); 

</script> 

感謝