将多态对象发布到控制器操作

时间:2016-07-15 10:06:05

标签: c# asp.net-mvc-4 polymorphism

我已成功将多态结构映射到视图中,但是,当我尝试发布特定派生对象的用户输入时,它无法识别控制器操作中的派生类型...

基本类型的顶级视图:

@model  InformationRequestViewModel

@using (Html.BeginForm("PostInformationRequest", "SomeController", FormMethod.Post))
{
    @Html.EditorForModel()
}

查看派生类型:

@model  ActiveServiceInformationRequestViewModel

<label>Start date:</label>
@Html.TextBoxFor(x => x.StartDate, new { @class = "u-full-width" })

<label>End date:</label>
@Html.TextBoxFor(x => x.EndDate, new { @class = "u-full-width" })

发布操作:

[HttpPost]
public ActionResult PostInformationRequest(InformationRequestViewModel model)
{
    if(model is ActiveServiceInformationRequestViewModel)
    {
        // IT IS NOT DIPPING INTO THIS LOGIC!

        // Do something with this particular concrete type...
    }

    // More derived type checks and relevant logic here...

    return RedirectToAction("Index");
}

基本视图模型:

public class InformationRequestViewModel
{
    public int Id {get; set;}
}

派生的视图模型:

    public class ActiveServiceInformationRequestViewModel : InformationRequestViewModel
    {
          public DateTime StartDate {get; set;}
          public DateTime EndDate {get; set;}
    }

0 个答案:

没有答案