这个问题真的很简单,但我似乎无法解决它。我正在使用带有 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 活页夹。
谢谢。