我无法调用我在Razor代码中编写的函数,我将参数传递给控制器。
任何人都有任何想法?
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)
最后,我自己找到了问题的答案:
你可以通过在调用Ajax func的jQuery中订阅它的事件来获取你正在使用的控件的值(在我的案例中是DropDownList),Ajax会将从事件中检索到的参数发布到Action而不是您可以在操作中使用它,如下所示:
<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"
}
)