MVC 3 DevExpress - 返回控制器的模型为空

时间:2012-07-03 22:16:02

标签: asp.net-mvc asp.net-mvc-3 devexpress

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

我有这段代码:

MODEL:

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

CONTROLLER

    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()
}

现在所有这一切都很简单,而且我没有做任何事情,只是出差。但是当我单击Save按钮时,我会回到控制器,这个方法:

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

            return View( model );
        }

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

感谢。

2 个答案:

答案 0 :(得分:0)

ComboBox的名称列出了类型定义。因为您传入 TestModel 作为类型,所以ComboBox的名称应该是应该保存角色ID的属性。所以绑定器试图填充 model.TestDx.Models.TestModel.Roles 但是找不到该字段,因此没有任何东西被填满。如果查看formCollection对象,您应该看到带有值的ComboBox。

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

 @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( )

这可能适用于您的绑定。

答案 1 :(得分:0)

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