MVC从Razor代码调用Javascript func
我无法从Razor代码中调用我在脚本中编写的func,在该代码中,我将参数传递给控制器 有人知道吗 霉菌代码:MVC从Razor代码调用Javascript func,javascript,c#,asp.net-mvc,razor,Javascript,C#,Asp.net Mvc,Razor,我无法从Razor代码中调用我在脚本中编写的func,在该代码中,我将参数传递给控制器 有人知道吗 霉菌代码: <script type="text/javascript"> function GetDropDownValue(ControlId) { var list = document.getElementById(ControlId); return list.options[list.selectedIndex].value; } 函数Ge
<script type="text/javascript">
function GetDropDownValue(ControlId) {
var list = document.getElementById(ControlId);
return list.options[list.selectedIndex].value;
}
函数GetDropDownValue(ControlId){
var list=document.getElementById(ControlId);
返回list.options[list.selectedIndex].value;
}
@Html.DropDownList(
“TransactionType”,
新建SelectList(Enum.GetValues(typeof(Forex.Reports.ReportValueType)),
“交易类型”,
新的
{
@class=“表单控制”,
@id=“类型”
})
//这是我尝试调用Javascript函数的参数
@使用(Html.BeginForm(“货币交易”,“交易”),新{type=
“GetDropDownValue('type')”})
{
}
谢谢,
雅尼夫最后,我自己找到了问题的答案: 通过在调用Ajax函数的jQuery中订阅控件的事件,您可以获得正在使用的控件的值(在我的例子中是DropDownList),Ajax将把从事件中检索到的参数发布到操作中,然后您可以在操作中使用它,如下所示:
<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"
}
)
//定义jqueryfunc
jQuery(文档).ready(函数($){
//按控件的#id订阅控件事件
$(“#type”).change(函数(){
//定义ajax函数
$.ajax({
//设置用于发布的url
url:“@url.Action”(“货币交易”、“交易”)”,
//将Http请求设置为Post
键入:“POST”,
cache:false,
//将控件选择的值过帐到操作
数据:{选定:$(“#类型”).val()},
成功:功能(数据){
//成功代码(如果需要)
}
});
});
});
//下拉列表控件
@Html.DropDownList(
“TransactionType”,
新建SelectList(Enum.GetValues(typeof(Forex.Reports.ReportValueType)),
“交易类型”,
新的
{
@class=“表单控制”,
@id=“类型”
}
)
最后,我自己找到了问题的答案:
通过在调用Ajax函数的jQuery中订阅控件的事件,您可以获得正在使用的控件的值(在我的例子中是DropDownList),Ajax将把从事件中检索到的参数发布到操作中,然后您可以在操作中使用它,如下所示:
<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"
}
)
//定义jqueryfunc
jQuery(文档).ready(函数($){
//按控件的#id订阅控件事件
$(“#type”).change(函数(){
//定义ajax函数
$.ajax({
//设置用于发布的url
url:“@url.Action”(“货币交易”、“交易”)”,
//将Http请求设置为Post
键入:“POST”,
cache:false,
//将控件选择的值过帐到操作
数据:{选定:$(“#类型”).val()},
成功:功能(数据){
//成功代码(如果需要)
}
});
});
});
//下拉列表控件
@Html.DropDownList(
“TransactionType”,
新建SelectList(Enum.GetValues(typeof(Forex.Reports.ReportValueType)),
“交易类型”,
新的
{
@class=“表单控制”,
@id=“类型”
}
)
是否尝试在表单提交时获取所选下拉列表项?是否尝试在表单提交时获取所选下拉列表项?