温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c# - Access common view for different models with same columns
.net asp.net-mvc c# razor view

c# - 使用相同的列访问不同模型的通用视图

发布于 2020-04-04 10:58:17

我正在开发一个MVC应用程序,该应用程序具有阶段D1,D2,D3和D4,我们将从应用程序中加载/编辑每个阶段。

现在,我为每个阶段模型创建了单独的模型和视图,如下所示,并加以利用。但是对于代码重用,我想对所有阶段(D1,D2,D3和D4)使用通用视图,因为所有阶段都包含相同的列。

我有一个想法,可以为所有四个阶段创建一个单一的视图模型并加以利用,但是我们需要分别编辑和保存这些值。所以我对此感到震惊。是否有任何想法通过加载和保存每个阶段的数据来对所有四个模型使用通用视图。

实体

public partial class D1Stage : EntityBase
{
    [DisplayName("Status Indicator")]
    public byte StatusIndicatorId { get; set; }

    [DisplayName("Status Info")]
    public string StatusInfo { get; set; }

    [DisplayName("Completed %")]
    [SCIRange(0, 100)]
    public decimal StageCompletion { get; set; }

    [DisplayName("Is Step Mandatory")]
    public bool IsMandatory { get; set; }
}

正如我所提到的,D2StageD3Stage并且D4Stage具有完全相同的特性,例如用于D2Stage

public partial class D2Stage : EntityBase
{
    ... exact same properties as D1Stage 
}

观看次数

@model Brain.DAL.Entities.D1Stage
@{
    IEnumerable<SelectListItem> StatusIndicators = ViewBag.StatusIndicators;
}

@using (Html.BeginForm("", "", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form form-horizontal" }))
{
    <div class="form-body">
        @Html.AntiForgeryToken()
        @Html.HiddenFor(model => model.Id)
        @Html.HiddenFor(model => model.Report.Id)
        <div class="form-group">
            @Html.SCILabelFor(model => model.StatusIndicatorId, new { @class = "col-md-1 control-label" })
            <div class="input-group col-md-4">
                @Html.DropDownListFor(model => model.StatusIndicatorId, new SelectList(StatusIndicators, "Value", "Text"), "None", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.StatusIndicatorId, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.SCILabelFor(model => model.StatusInfo, new { @class = "col-md-1 control-label" })
            <div class="input-group col-md-4">
                @Html.TextAreaFor(model => model.StatusInfo, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.StatusInfo, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.SCILabelFor(model => model.StageCompletion, new { @class = "col-md-1 control-label" })
            <div class="input-group col-md-4">
                @Html.SCINumericTextBoxFor(model => model.StageCompletion, new { @class = "form-control sliderrangemintext", @style = "width:100px" })

                <div class="sliderrangemin" id="slider-range-min" style="margin-top:50px"></div>
            </div>            
        </div>
        <div class="form-group">
            @Html.SCILabelFor(model => model.IsMandatory, new { @class = "col-md-1 control-label" })
            <div class="input-group col-md-4">
                <div class="input-group" style="width:100%">
                    @Html.DropDownListFor(model => model.IsMandatory, new List<SelectListItem>() { new SelectListItem { Text = "No", Value = "false" }, new SelectListItem { Text = "Yes", Value = "true", Selected = true } }, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.IsMandatory, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
    </div>
}

正如我上面提到的,所有其他视图都具有相同的内容,只是具有不同的对应模型。例如D2Stage

@model Brain.DAL.Entities.D2Stage
... The difference is just in model. 
... Rest of the content is exactly the same as D1Stage view

查看更多

提问者
Vignesh Kumar A
被浏览
138
Reza Aghaei 2020-01-27 18:51

所有型号都有相似的特性吗?假设您无法创建单个模型。然后将属性移至基类并创建从基模型派生的其他模型。然后,还创建一个以基本模型为模型的单一视图。

楷模

public class MyBaseModel
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

public class ModelA: MyBaseModel{}
public class ModelB: MyBaseModel{}

视图

为创建一个视图,MyBaseModel然后将视图放入Shared文件夹中,并命名为MySharedView

@model MyNamespace.MyBaseModel

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Title which you can decide based on model type or get from ViewBag</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Property1, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Property1, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Property1, "", new { @class = "text-danger" })
            </div>
        </div>

        ... Rest of properties ...

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

控制者

然后在控制器中,当您想返回视图时,指定共享视图名称。例如,模型A和模型相同B

[HttpGet]
public ActionResult A()
{
    var model = new ModelA() { Property1 = "A", Property2 = "A" };
    return View("MySharedView", model);
}
[HttpPost]
public ActionResult A(ModelA model)
{
    // SaveA(model)
    // Redirect to index
}