MVC Call Javascript func from Razor code



我无法调用我从剃须刀代码中写入参数到控制器的脚本中写的func。

有人有任何想法吗?

mycode:

<script type="text/javascript">
    function GetDropDownValue(ControlId) {
    var list = document.getElementById(ControlId); 
    return list.options[list.selectedIndex].value;
} 

@Html.DropDownList(
   "TransactionType",
   new SelectList(Enum.GetValues(typeof(Forex.Reports.ReportValueType))),
   "Transaction Type",
   new
   {
       @class = "form-control",
       @id = "type"
   })
//This is the parameter where I try to invoke the Javascript func                                    
@using (Html.BeginForm("MonetaryTransactions", "Trading",new { type =
"GetDropDownValue('type')" }))
{
<input type="submit" value="D"/>
}

谢谢yaniv

最后,我自己找到了我的问题的答案:

您可以通过在调用AJAX函数的jQuery中订阅其事件来获取所使用的控件的值(在我的情况下)在以下操作中:

<script language="javascript" type="text/javascript">
     // Defining jquery func
     jQuery(document).ready(function ($) {
         // Subscribing the control event by it's #id
         $("#type").change(function () {
             // Defining ajax func
             $.ajax({
                 // Setting url for posting
                 url: "@Url.Action("MonetaryTransactions", "Trading")",
                 // Setting the Http Request to Post
                 type: 'POST',
                 cache: false,
                 // Posting the control seleced value to the action
                 data: { Selected: $("#type").val() },
                 success: function (data) {
                     // success code if needed
                 }
             });
         });
     }); 
</script>
// The DropDownList control
@Html.DropDownList(
   "TransactionType",
   new SelectList(Enum.GetValues(typeof(Forex.Reports.ReportValueType))),
   "Transaction Type",
   new
   {
       @class = "form-control",
       @id = "type"
   }
)

最新更新