在我的 ASP.Net MVC3 项目中,我创建了一个绑定基本模型的 ModelBinder。在我的视图中,我从从我的基本模型继承的模型创建一个对象。现在,当我按下提交按钮时,我想知道哪个模型是通过我的 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