2012-12-02 38 views
0

這是我的視圖代碼的一部分:點擊第三它變量未分配使用asp.net MVC

<%int imageTypSelected = 0; %> 



<div class="imageToEdit" > 
     <img src='<%: Url.Action("getImg", "Product", new{ShopId = Model.Id}) %>' alt="" /> 
     <a href="#" id="1" onclick="jQuery('#dialog').dialog('open'); '<%:imageTypSelected=1 %>'; return false; ">G</a>    
    </div> 

    <div > 
     <img src='<%: Url.Action("getImg_Alt1", "Product", new{ShopId = Model.Id}) %>' alt="" /> 
     <a href="#" id="2" onclick="jQuery('#dialog').dialog('open'); '<%:imageTypSelected=2 %>'; return false; " >G</a>   
    </div> 

    <div > 
     <img src='<%: Url.Action("getImg_Alt2", "Product", new{ShopId = Model.Id}) %>' alt="" /> 
     <a href="#" id="3" onclick="jQuery('#dialog').dialog('open'); '<%:imageTypSelected=3 %>'; return false; ">G</a>    
    </div> 



<div id="dialog" title="A" > 
      <% using (Html.BeginForm("changeProductImage", "Product", new { @Id = Model.Id, @selectedHyperLink = imageTypSelected }, FormMethod.Post, new { enctype = "multipart/form-data" })) 
       {%> 
       <p><input type="file" id="fileUpload" name="fileUpload" style="width:23;"/> </p> 
       <p><input type="submit" value="B" /></p> 
      <% } %> 

     </div> 

通過點擊第一可變imageTypSelected必須設置爲1。通過第二必須設置爲3,並通過必須設置爲3.然後將這個變量傳遞給控制器​​中的一個方法。但一直只有值3分配給變量,點擊其他不會影響變量。哪裏不對?

回答

1

你混合了服務器端代碼和客戶端代碼。

imageTypSelected只存在於服務器上。點擊瀏覽器中的鏈接後,它不會發生變化。您需要在JavaScript完全做到這一點:

刪除這從您的形式:

@selectedHyperLink = imageTypSelected 

,並添加這個隱藏字段:

<input type="hidden" id="selectedhyperlink" name="selectedhyperlink" /> 

添加此javascript函數將設置在隱藏字段正確的值:

<script type="text/javascript"> 
    function changeImageType(imageType) { 
     jQuery("#selectedhyperlink").val(imageType); 
     jQuery("#dialog").dialog("open"); 

     return false; 
    } 
</script> 

從您的鏈接調用此功能:

<a href="#" id="2" onclick="return changeImageType(2);">G</a>