编程语言
首页 > 编程语言> > c#-基于标志的条件绑定

c#-基于标志的条件绑定

作者:互联网

我有一个带有AJAX数据源的Kendo-UI网格.
我正在使用ASP.NET-MVC.

该模型如下所示:

public class QuestionModelPlayer
{
    public Guid Id { get; set; }
    public String Description { get; set; }
    public string TextAnswer { get; set; }
    public int? NummericAnswer { get; set; }
    public bool isTextQuestion { get; set; }
}

如果布尔IsTextQuestion为true,则我希望用户有一个绑定到TextAnswer字段的内嵌文本框.如果该值为false,我想将其绑定到NummericAnswer属性.

我怎样才能做到这一点?
我想我需要使用模板或ClientTemplate吗?

解决方法:

根据Telerik文档:

If the grid is ajax bound use the ClientTemplate method. The value should be a string which represents a valid Kendo Template.

几个摘录自其doco的代码片段大致适应了您的情况(但未经测试!)显示了如何做到这一点.首先作为一些内联javascript代码:

columns.Bound(q => q.isTextQuestion)
       .ClientTemplate (
    "# if (isTextQuestion == true) { #" +
        "#: TextAnswer #" +
    "# } else { #" +
        "#: NummericAnswer #" +
    "# } #"
);

或者通过调用javascript函数:

 columns.Bound(q => q.isTextQuestion)
        .ClientTemplate("#= getAnswer(data) #");


<script>
    function getAnswer(question) {

        var html = kendo.format( "<text>{0}</text>"
                                ,question.isTextQuestion 
                                    ? question.TextAnswer 
                                    : question.NummericAnswer 
                                );

        return html;
    }
</script>

查看常见问题解答项目Grid Frequently Asked Questions: Displaying Values,以获取更多示例.

标签:kendo-grid,c,asp-net-mvc,kendo-ui-mvc
来源: https://codeday.me/bug/20191027/1948348.html