变量不使用asp.net mvc分配

时间:2012-12-02 18:40:33

标签: 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 个答案:

答案 0 :(得分: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>