ASP.Net MVC3プロジェクトで、ベースモデルをバインドするModelBinderを作成しました。私のビューでは、ベースモデルから継承するモデルからオブジェクトを作成します。送信ボタンを押したときに、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