更好的方法概念在MVC的表单中使用具有继承的Model

时间:2018-02-04 12:39:45

标签: c# forms inheritance razor model-view-controller

我想在架构MVC中使用最好的技术,使用子类操作,我尝试不使用更多参数,只使用模型,我不能使用接口:

模型 - 父示例:

public class Animal
{
    public string Name { get; set; }
    public string Color { get; set;  }
}

模型 - 子示例:

public class Dog : Animal
{
    public String Hair {get;set;}
}

查看发件人示例SenderDog.cshtml:

@model WebAppHerencia.Models.Dog

@using(Html.BeginForm()) {

@Html.LabelForModel()
@Html.LabelFor(m=>m.Name)
@Html.TextBoxFor(m => m.Name)
@Html.LabelFor(m => m.Hair)
@Html.TextBoxFor(m => m.Hair)
<input name="submitSend" type="submit" value="send"/>

}

查看Receibe示例ReceiveDog.cshtml:

    @model WebAppHerencia.Models.Dog

<h2>Receive Dog</h2>
@Html.LabelForModel()
@Html.LabelFor(m=>m.Hair)
@Html.EditorFor(m=>m.Hair)

问题:控制器

    public ActionResult SendDog(Animal model)
    {
        if (Request.Form["submitSend"]!=null)
        {
            var p = model as Dog; 
            return RedirectToAction("ReceiveDog",p);
        }
        return View();
    }

    public ActionResult ReceiveDog(Dog perro)
    {
        return View(perro);
    }

我尝试在控制器的动作中发送Dog型的模型(SendDog(动物a))等动物(父母),我知道不能通过这种方式的孩子,但我不知道怎么做最好的方式......而不是使用另一个参数...

debug

我的脚手架示例 scaffolding

0 个答案:

没有答案