我正在开发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; }
}
public partial class D2Stage : 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; }
}
public partial class D3Stage : 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; }
}
public partial class D4Stage : 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; }
}
每个阶段的视图
D1阶段
@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>
}
D2阶段
@model Brain.DAL.Entities.D2Stage
@{
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>
}
D3Satge
@model Brain.DAL.Entities.D3Stage
@{
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>
}
D4阶段
@model Brain.DAL.Entities.D4Stage
@{
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>
}
所有型号都有相似的特性吗?假设您无法创建单个模型。然后将属性移至基类并创建从基模型派生的其他模型。然后还创建一个以基本模型为模型的单一视图。
模型
public class MyBaseModel
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
public class ModelA: MyBaseModel{}
public class ModelB: MyBaseModel{}
查看
将视图放置在Shared
文件夹中并命名为MySharedView
。
@model MyNamespace.MyBaseModel
@using (Html.BeginForm())
{
<div class="form-body">
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(model => model.Property1)
<div class="input-group col-md-4">
@Html.EditorFor(model => model.Property1)
@Html.ValidationMessageFor(model => model.Property1)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Property2)
<div class="input-group col-md-4">
@Html.EditorFor(model => model.Property2)
@Html.ValidationMessageFor(model => model.Property2)
</div>
</div>
</div>
}
Controller
然后在控制器中,当您想返回视图时:
return View("MySharedView", model);