Dev 快速网格视图组合框在绑定值为 null/空时默认值

本文关键字:null 默认值 绑定 网格 视图 组合 Dev | 更新日期: 2023-09-27 17:55:26

以下是我在开发快速网格视图编辑模板中使用的组合框代码。我希望组合框在添加新员工时根本不显示任何内容或显示"请选择和员工"的字符串,而不是在编辑时显示。

        @Html.DevExpress().ComboBox(edtSettings =>
            {
                edtSettings.Width = 200;
                edtSettings.Name = "EmployeeID";
                //edtSettings.Properties.TextField = "FirstName";
                edtSettings.Properties.ValueField = "EmployeeID";
                edtSettings.Properties.ValueType = typeof(int);
                edtSettings.Properties.DropDownStyle = DropDownStyle.DropDownList;
                edtSettings.ShowModelErrors = true;
                edtSettings.Properties.TextFormatString = "{0}" + " " + "{1}";
                edtSettings.Properties.Columns.Add("FirstName", "First Name", 150);
                edtSettings.Properties.Columns.Add("LastName", "Last Name", 150);
                //--- For client side validations
                edtSettings.Properties.ValidationSettings.RequiredField.IsRequired = true;
                edtSettings.Properties.ValidationSettings.RequiredField.ErrorText = "An Employee must be selected from the list.";
            }).BindList(Model.NonUsers).Bind(Model.UserSingle.EmployeeID).GetHtml()

我这样做是为了传递正确的Model.UserSingle,如下所示

    var User = ViewData["UserDetail"] != null
                            ? ViewData["UserDetail"]
                            : !c.Grid.IsNewRowEditing
                                ? Model.UserList.FirstOrDefault(m => m.EmployeeID == Convert.ToInt32(DataBinder.Eval(c.DataItem, "EmployeeID")))
                                : new User
                                    {
                                        SiteID = (int)Session["siteID"],
                                        ////employee ID cant set to blank, keeps adding a zero
                                        //EmployeeID = Convert.ToInt32(null),
                                        //this is defaulted to 'Timesheet Only'
                                        UserGroupID = 3
                                    };
            Model.UserSingle = (User)User;

Dev 快速网格视图组合框在绑定值为 null/空时默认值

目前 MVC ComboBox 扩展不允许将"空项"(空文本、水印)添加到项集合中。它位于DevEx TO-DO列表中。

您可以尝试此解决方法。

 @Html.DevExpress().ComboBox(edtSettings =>
            {
    edtSettings.Properties.ClientSideEvents.Init = "function(s,e){if(s.GetValue()=='0'){ s.SetValue(null);} }";
...

在组合框中,可以使用此属性:

edtSettings.Properties.NullDisplayText = "Please Choose and Employee";