英文:
Variables are not accessible inside Action Url - kendo dropdown
问题
我在asp.net mvc中的script
标签内有一个Kendo Html.Kendo().ComboBox()
。
var input = '@(Html.Kendo().ComboBox()
.Name(Html.NameFor(model => model.AttributeUnitValue).ToString())
.DataTextField("Description")
.DataValueField("CodeLovId")
.DataSource(datasource => datasource
.Read(read => read
.Action("GetCodesList", "LookupCode", new { area = "Core", codeType = unitCodeType, selectedValue = minAccValue })
.Type(HttpVerbs.Post)
)
).HtmlAttributes(new { style = "width:50%" }))';
在此输入控件之外,我有两个变量unitCodeType
和minAccValue
,但我无法在给定的代码中访问它们。它们显示错误。请查看下面的屏幕截图。
如何修复这个问题?
英文:
I have a Kendo Html.Kendo().ComboBox()
inside script
tag in asp.net mvc.
var input = '@(Html.Kendo().ComboBox()
.Name(Html.NameFor(model => model.AttributeUnitValue).ToString())
.DataTextField("Description")
.DataValueField("CodeLovId")
.DataSource(datasource => datasource
.Read(read => read
.Action("GetCodesList", "LookupCode", new { area = "Core", codeType = unitCodeType, selectedValue = minAccValue })
.Type(HttpVerbs.Post)
)
).HtmlAttributes(new { style = "width:50%" }))'
Out side this input control I have two variables unitCodeType
and minAccValue
, which I am not able to access in Action()
in the given code. They are showing error. Please check below screen shot
How can I fix this ?
答案1
得分: 1
你可以将服务器端变量传递给HtmlHelper的Action()方法。Html助手在服务器上进行评估,即根据流畅的配置创建初始化脚本并输出,以及用于组件初始化的元素。因此,在Html助手评估时,你尝试传递的JavaScript变量在上下文中不可用。
你有两个选项 - 使用服务器端变量或使用JS初始化:
@{
var someParam = 3;
}
<label for="products">HtmlHelper:</label>
@(Html.Kendo().ComboBox()
.Name("products")
.DataTextField("ProductName")
.DataValueField("ProductID")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("RemoteDataSource_GetProducts", "DropDownList", new { myParam = @someParam });
});
})
.HtmlAttributes(new { style = "width: 200px;" })
)
<label for="products">JS initialization:</label>
<input id="products_js" style="width:200px;"/>
<script>
var someOtherParam = "test";
$("#products_js").kendoComboBox({
dataTextField: "ProductName",
dataValueField: "ProductID",
dataSource: {
transport: {
read: {
dataType: "jsonp",
url: "https://demos.telerik.com/kendo-ui/service/Products",
data:{
myOtherParam:someOtherParam
}
}
}
}
});
</script>
如果你检查这个示例中的Network选项卡,你会看到传递给read端点的不同参数。
英文:
You can pass server-side variables to the Action() method of the HtmlHelper. The Html helper is evaluated on the server i.e. based on the fluent configuration an initialization script is created and output along with an element used for the initialization of the component. So the JavaScript variable you are trying to pass is not available in the context when the Html Helper is evaluated.
You have two options - use server-side variables or initialize the ComboBox using JS:
@{
var someParam = 3;
}
<label for="products">HtmlHelper:</label>
@(Html.Kendo().ComboBox()
.Name("products")
.DataTextField("ProductName")
.DataValueField("ProductID")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("RemoteDataSource_GetProducts", "DropDownList",new { myParam = @someParam});
});
})
.HtmlAttributes(new { style = "width: 200px;" })
)
<label for="products">JS initialization:</label>
<input id="products_js" style="width:200px;"/>
<script>
var someOtherParam = "test";
$("#products_js").kendoComboBox({
dataTextField: "ProductName",
dataValueField: "ProductID",
dataSource: {
transport: {
read: {
dataType: "jsonp",
url: "https://demos.telerik.com/kendo-ui/service/Products",
data:{
myOtherParam:someOtherParam
}
}
}
}
});
</script>
If you inspect the Network tab in this example you will see the different parameters passed to the read endpoint.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论