变量在操作 URL 中不可访问 – Kendo 下拉菜单

huangapple go评论44阅读模式
英文:

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%" }))';

在此输入控件之外,我有两个变量unitCodeTypeminAccValue,但我无法在给定的代码中访问它们。它们显示错误。请查看下面的屏幕截图。

变量在操作 URL 中不可访问 – Kendo 下拉菜单

如何修复这个问题?

英文:

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

变量在操作 URL 中不可访问 – Kendo 下拉菜单

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

&lt;label for=&quot;products&quot;&gt;HtmlHelper:&lt;/label&gt;
@(Html.Kendo().ComboBox()
    .Name(&quot;products&quot;)
    .DataTextField(&quot;ProductName&quot;)
    .DataValueField(&quot;ProductID&quot;)
    .DataSource(source =&gt;
    {
        source.Read(read =&gt;
        {
            read.Action(&quot;RemoteDataSource_GetProducts&quot;, &quot;DropDownList&quot;,new { myParam = @someParam});
        });
    })
    .HtmlAttributes(new { style = &quot;width: 200px;&quot; })
)

&lt;label for=&quot;products&quot;&gt;JS initialization:&lt;/label&gt;
&lt;input id=&quot;products_js&quot; style=&quot;width:200px;&quot;/&gt;

&lt;script&gt;
var someOtherParam = &quot;test&quot;;
 $(&quot;#products_js&quot;).kendoComboBox({
        dataTextField: &quot;ProductName&quot;,
        dataValueField: &quot;ProductID&quot;,
        dataSource: {
            transport: {
                read: {
                    dataType: &quot;jsonp&quot;,
                    url: &quot;https://demos.telerik.com/kendo-ui/service/Products&quot;,
                    data:{
                        myOtherParam:someOtherParam
                    }
                }
            }
        }
    });

</script>

If you inspect the Network tab in this example you will see the different parameters passed to the read endpoint.

huangapple
  • 本文由 发表于 2023年2月14日 21:06:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定