2011-06-13 110 views
4

在我的ASP.Net MVC3項目中,我創建了一個綁定基本模型的ModelBinder。在我的視圖中,我從模型中創建一個對象,從我的基本模型繼承。現在我不想知道哪個模型是通過我的ModelBinder中的反射創建的,當我按下提交按鈕時,但是如何?如何將http請求轉換爲正確的對象?

ModelBinder的:

public class MBTestBinder : IModelBinder 
{ 
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     //need to know which Model was created -> convert into the right object 
     //reflection? 
    } 
} 

型號:

[ModelBinder(typeof(MBTestBinder))] 
public class MBTest 
{ 
    public string Name { get; set; } 
    public MBTest() {} 
} 

public class MBAbl : MBTest 
{ 
    public MBAbl() {} 
    public string House { get; set; } 
} 

查看:

@model ModelBinderProject.Models.MBTest 

@using (Html.BeginForm("Index", "Home")) { 
<fieldset> 
    <div class="editor-field"> 
     @Html.EditorForModel(Model) 
    </div> 
    <p> 
     <input type="submit" value="Create" /> 
    </p> 
</fieldset> 

控制器:

public ActionResult Create(MBTest testItem) 
{ 
    //on init get a view from a class that hast inherit the class MBTest 
    if (testItem.Name == null) testItem = new MBAbl(); 

    return View(testItem); 
} 

編輯:

bindingContext.ValueProvider.GetValue("House")我可以得到表的價值,但bindingContext.ModelType認爲我的模型是MBTest

回答

0

Finaly我在我的模型攜帶模型的名稱,並動態地創建在ModelBinder的正確的模式的解決辦法解決它。 如果你知道一個更好的解決方案PLZ告訴我:-)

的HomeController:

// CREATE 
public ActionResult About(MBTest testItem) 
{ 
    if (testItem == null) 
    { 
     testItem = new MBAbl(); 
     testItem.Model = "MBAbl"; 
    } 

    return View(testItem); 
} 

型號:

public class MBTest 
{ 
    public MBTest() {} 

    [HiddenInput] 
    public string Model { get; set; } 

    public string Name { get; set; } 
} 

public class MBAbl : MBTest 
{ 
    public MBAbl() {} 

    public string House { get; set; } 
} 

public class MBAb2 : MBTest 
{ 
    ... 
} 

ModelBinder的:

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
{ 
    if (controllerContext == null) throw new ArgumentNullException("controllerContext"); 
    if (bindingContext == null) throw new ArgumentNullException("bindingContext"); 

    //string 'Model' is needed in the base class 
    var modelType = bindingContext.ValueProvider.GetValue("Model"); 

    if (modelType != null && !string.IsNullOrEmpty(modelType.AttemptedValue)) 
    { 
     string projectName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; 

     Type classtype = Type.GetType(string.Format("{0}.Models.{1}", projectName, modelType.AttemptedValue)); 
     PropertyInfo[] properties = classtype.GetProperties(); 

     var classObject = classtype.GetConstructor(new Type[] { }).Invoke(null); 

     foreach (PropertyInfo propertie in properties) 
     { 
      var value = bindingContext.ValueProvider.GetValue(propertie.Name).AttemptedValue; 
      classtype.GetProperty(propertie.Name).SetValue(classObject, value, null); 
     } 

     return classObject; 
    } 
    return null; 
} 
0

檢查ModelBindingContext文檔。

編輯基於評論

public class MBTestBinder : IModelBinder 
{ 
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     var result = bindingContext.ValueProvider.GetValue("Name"); 

     if (result == null || string.IsNullOrEmpty(result.AttemptedValue)) 
      return new MBAbl(); 
     else 
      return new MBTest(); 
    } 
} 
+0

'''''bindingContext.ModelType'只返回基類 - 沒有工作:-( – jwillmer 2011-06-14 10:40:10

+0

@myName更新回答 – Eranga 2011-06-14 14:46:47

+0

@Eranga'UpdateModel(..)'再次調用ModelBinder,所以ModelBinder獲取'bindingContext.Model' 。但是,這意味着這種觀點。 第一次查看有兩種形式(House&Name)。當我按提交時,模型綁定器不檢測繼承類,只有基礎模型。 – jwillmer 2011-06-14 15:00:37

0

試試這個:

public class ModelBinder : DefaultModelBinder, IModelBinder 
{ 
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     var typeValue = bindingContext.ValueProvider.GetValue("ModelType"); 
     var type = Type.GetType("Namespace.a.b." + typeValue.AttemptedValue.ToString()); 

     var model = Activator.CreateInstance(type); 

     //Change the model 
     bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, type); 
     bindingContext.ModelMetadata.Model = model; 

     //Here, we used the default model binder of the mvc 
     return base.BindModel(controllerContext, bindingContext);; 
    } 
} 
+0

您應該爲您的答案添加說明,而不僅僅是發佈代碼。 – 2012-10-11 05:43:44