2017-03-15 25 views
1

我无法调用我在Razor代码中编写的函数,我将参数传递给控制器​​。MVC从剃刀代码调用Javascript 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

+0

你想获得选择表单提交下拉项目? –

回答

0

最后,我发现自己对我的问题的答案:

你可以得到的值你正在使用的控件(在我的情况下DropDownList)通过订阅它的前夕NT jQuery中调用Ajax的FUNC按键,阿贾克斯将公布该事件的行动检索的参数和比你可以在动作按如下方式使用它:

<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" 
    }