MVC5模型继承表单

时间:2017-06-30 09:07:36

标签: c# inheritance model asp.net-mvc-5

我目前在开发过程中遇到了MVC5的问题,并且我正在寻找这个问题的解决方案。 为了解释我的问题,我将简化它:

目前我有两个继承的类

public class Person
{
    public string PersonID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Professor : Person
{
    public string Education { get; set; }
}

public class Student : Person
{
    public string Grade { get; set; }
}

我有一个输入信息的表单页面:

@model Person

@using (Html.BeginForm("Creation", "Person", FormMethod.Post, new { id = "form0" }))
{
  <button type="submit" class="btn btn-primary" id="Save" name="actionName">SAVE</button>


    <div class="tab-content">

        <div id="infogene">
            @Html.Partial("~/Views/Person/GeneralInformation.cshtml", Model)
        </div>

        @if ((bool)Session["ProductCodeIsReadOnly"])
        {
            <div id="infospec" class="">
                @if (Model.PersonID.StartsWith("P"))
                {
                    @Html.Partial("~/Views/Person/SpecialInformationProfessor.cshtml", AutoMapper.Mapper.Map<Professor>(Model))
                }
                else if (Model.PersonID.StartsWith("S"))
                {
                    @Html.Partial("~/Views/Person/SpecialInformationStudent.cshtml", AutoMapper.Mapper.Map<Student>(Model))
                }
            </div>
        }
    </div>
}

SpecialInformationProfessor / SpecialInformationStudent是一个需要教授/学生模型但基本视图是人的视图。

这是我的行动

public ResultCode Creation<T>(T Person, string actionName) where T: Person
{
 //Here i need to get Person as an Professor(if the partial was the Professor One) or as an Student(if the partial was the Student One) but i get only the person information
 return ResultCode.OK;
}

提前谢谢。

PS:这段代码是一个概念(针对这个问题而制作)。

0 个答案:

没有答案