2012-07-03 65 views
2

問題很簡單,但我似乎無法解決它。我使用DevExpress組合框使用Razor引擎。MVC 3 DevExpress - 返回控制器的模型爲空

我有這樣的代碼:

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

控制器

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

在這裏,我可以成功打開視圖,並且精細顯示的數據:

VIEW

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

現在,這一切都非常簡單,我沒有做任何事的,只是dispaying它。但是,當我點擊保存按鈕,我回到控制器,這種方法:

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

      return View(model); 
     } 

...和型號這裏是空的,角色屬性爲0。我不明白這是爲什麼?事件組合框名稱與它所綁定的屬性相同,我確定使用推薦的DevExpress活頁夾。

謝謝。

+1

如果知道相同的問題,可以很好地知道原因。 –

回答

0

ComboBox的名稱列出了類型定義。因爲您傳遞的是TestModel作爲類型,所以ComboBox的名稱應該是應該保存角色ID的屬性。所以活頁夾試圖填充model.TestDx.Models.TestModel.Roles,但無法找到該字段,因此沒有任何內容被填充。如果你看看formCollection對象,你應該看到你的ComboBox的值。

一個更好的解決辦法是有角色ID在TestModel指定,並命名該組合框只是角色ID。在這種情況下,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() 

這可能適用於您的綁定。

0

該模型未被綁定。您的RoleID名稱屬性必須與Roles [listIndex] .RoleID不同。 要將列表綁定到您的模型中,輸入名稱應該是ListPropName[listIndex].PropNameInsideList

相關問題