2

这个问题真的很简单,但我似乎无法解决它。我正在使用带有 DevExpress 组合框的 Razor 引擎。

我有这个代码:

模型:

public class TestModel
{
    public string Name { get; set; }
    public List<Role> Roles { get; set; }
}

public class Role
{
    public int RoleId { get; set; }
    public string RoleName { get; set; }
}

控制器

    public ActionResult OpenTest()
    {
        TestModel tm = new TestModel( );
        tm.Roles = new List<Role>( );

        tm.Roles.Add( new Role( ) { RoleId = 1, RoleName = "Role 1" } );
        tm.Roles.Add( new Role( ) { RoleId = 2, RoleName = "Role 2" } );
        tm.Roles.Add( new Role( ) { RoleId = 3, RoleName = "Role 3" } );

        return View( tm );
    }

在这里我可以成功打开视图,并且数据显示正常:

看法

@model TestDx.Models.TestModel
@{
    ViewBag.Title = "OpenTest";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using ( Html.BeginForm( ) )
{
                        @Html.DevExpress( ).ComboBox(
                    settings =>
                    {
                        settings.Name = "TestDx.Models.TestModel.Roles";
                        settings.Width = 120;
                        settings.SelectedIndex = 0;
                        settings.Properties.DropDownWidth = 300;
                        settings.Properties.DropDownStyle = DevExpress.Web.ASPxEditors.DropDownStyle.DropDownList;
                        settings.Properties.EnableCallbackMode = false;
                        settings.Properties.CallbackPageSize = 30;
                        settings.Properties.IncrementalFilteringMode = DevExpress.Web.ASPxEditors.IncrementalFilteringMode.StartsWith;
                        settings.Properties.TextFormatString = "{0}";
                        settings.Properties.ValueField = "RoleID";
                        settings.Properties.ValueType = typeof( int );

                        settings.Properties.Columns.Add( "RoleID", "RoleID", 10 );
                        settings.Properties.Columns.Add( "RoleName", "RoleName", 100 );

                    } ).BindList( Model.Roles ).GetHtml( )
        <br />
            @Html.DevExpress().Button(
                    settings =>
                    {
                        settings.Name = "btnSave";
                        settings.ClientEnabled = true;
                        settings.ControlStyle.CssClass = "button";
                        settings.ClientVisible = true;

                        settings.Text = "save";
                        settings.UseSubmitBehavior = true;
                        settings.ControlStyle.Font.Bold = true;
                    }).GetHtml()
}

现在这一切都非常简单,我没有用它做任何事情,只是展示它。但是当我点击保存按钮时,我回到控制器,使用这个方法:

[HttpPost]
        public ActionResult OpenTest( [ModelBinder( typeof( DevExpressEditorsBinder ) )]TestModel model )
        {
            if ( ModelState.IsValid )
            {
               //
            }

            return View( model );
        }

...这里的模型是空的,角色属性是 0。我不明白为什么会这样?事件组合框名称与其绑定的属性相同,我确信使用推荐的 DevExpress 活页夹。

谢谢。

4

2 回答 2

0

模型未绑定。您的 RoleID 名称属性必须不同于 Roles[listIndex].RoleID。要将列表绑定到您的模型中,输入名称应为ListPropName[listIndex].PropNameInsideList

于 2013-05-15T20:03:51.653 回答
0

您的 ComboBox 的名称列出了类型定义。因为您将TestModel作为类型传递,所以您的 ComboBox 的名称应该是应该保存角色 Id 的属性。所以活页夹正在尝试填充model.TestDx.Models.TestModel.Roles但找不到该字段,因此没有填充任何内容。如果您查看 formCollection 对象,您应该会看到带有值的 ComboBox。

更好的解决方案是在TestModel中指定RoleID,并将 ComboBox 命名为RoleID。在这种情况下,RoleID 应该会自动填充到您的保存中。或者,您可以在视图中指定 Bind 字段,例如:

 @Html.DevExpress( ).ComboBox(
                    settings =>
                    {
                        settings.Name = "TestDx.Models.TestModel.Roles";
                        settings.Width = 120;
                        settings.SelectedIndex = 0;
                        settings.Properties.DropDownWidth = 300;
                        settings.Properties.DropDownStyle = DevExpress.Web.ASPxEditors.DropDownStyle.DropDownList;
                        settings.Properties.EnableCallbackMode = false;
                        settings.Properties.CallbackPageSize = 30;
                        settings.Properties.IncrementalFilteringMode = DevExpress.Web.ASPxEditors.IncrementalFilteringMode.StartsWith;
                        settings.Properties.TextFormatString = "{0}";
                        settings.Properties.ValueField = "RoleID";
                        settings.Properties.ValueType = typeof( int );

                        settings.Properties.Columns.Add( "RoleID", "RoleID", 10 );
                        settings.Properties.Columns.Add( "RoleName", "RoleName", 100 );

                    } ).BindList( Model.Roles )
                       .Bind(model.RoleID).GetHtml( )

这可能适用于您的绑定。

于 2013-05-15T19:04:31.713 回答