C# 访问具有相同列的不同模型的公共视图
我正在开发一个MVC应用程序,它有D1、D2、D3和D4阶段,我们将从应用程序中加载/编辑每个阶段 现在,我已经为每个阶段模型创建了单独的模型和单独的视图,如下所示,并利用它。但对于代码重用,我希望对所有阶段(D1、D2、D3和D4)使用公共视图,因为所有阶段都包含相同的列 我有一个想法,通过为所有四个阶段创建一个单一的视图模型并利用它们来实现,但我们需要分别编辑和保存这些值。因此,我对这一点印象深刻。是否可以通过加载和保存每个阶段的数据,为所有四个模型使用一个通用视图 实体C# 访问具有相同列的不同模型的公共视图,c#,.net,asp.net-mvc,razor,view,C#,.net,Asp.net Mvc,Razor,View,我正在开发一个MVC应用程序,它有D1、D2、D3和D4阶段,我们将从应用程序中加载/编辑每个阶段 现在,我已经为每个阶段模型创建了单独的模型和单独的视图,如下所示,并利用它。但对于代码重用,我希望对所有阶段(D1、D2、D3和D4)使用公共视图,因为所有阶段都包含相同的列 我有一个想法,通过为所有四个阶段创建一个单一的视图模型并利用它们来实现,但我们需要分别编辑和保存这些值。因此,我对这一点印象深刻。是否可以通过加载和保存每个阶段的数据,为所有四个模型使用一个通用视图 实体 public pa
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; }
}
@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>
}
public class MyBaseModel
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
public class ModelA: MyBaseModel{}
public class ModelB: MyBaseModel{}
正如我提到的,D2Stage
、D3Stage
和D4Stage
具有完全相同的属性,例如D2Stage
:
public partial class D2Stage : EntityBase
{
... exact same properties as D1Stage
}
视图
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; }
}
@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>
}
public class MyBaseModel
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
public class ModelA: MyBaseModel{}
public class ModelB: MyBaseModel{}
所有模型都有相似的属性?假设无法创建单个模型。然后将属性移动到基类,并创建从基类派生的其他模型。然后还创建一个以基本模型为模型的单一视图 型号
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; }
}
@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>
}
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>
}
所有模型都有相似的属性?假设无法创建单个模型。然后将属性移动到基类,并创建从基类派生的其他模型。然后还创建一个以基本模型为模型的单一视图 型号
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; }
}
@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>
}
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>
}
似乎您应该使用模式模型工厂。 使用属性和方法创建接口IStage(仅声明)。然后所有的类D*阶段都实现了IStage。因此,视图具有作为模型的IStage。 通过这种方式,每个类可以在每个方法中做不同的事情
interface IStage{
byte StatusIndicatorId { get; set; }
string StatusInfo { get; set; }
decimal StageCompletion { get; set; }
bool IsMandatory { get; set; }
}
public partial class D1Stage : EntityBase, IStage
{
[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; }
}
@看法
@model-IStage
@{
IEnumerable StatusIndicators=ViewBag.StatusIndicators;
}
@使用(Html.BeginForm(“,”,FormMethod.Post,new{enctype=“multipart/formdata”,
@class=“form horizontal”})
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model=>model.Id)
@Html.HiddenFor(model=>model.Report.Id)
@SCILabelFor(model=>model.statusindicator,新的{@class=“col-md-1控制标签”})
@Html.DropDownListFor(model=>model.statusindicator,new SelectList(StatusIndicators,“Value”,“Text”),“None”,new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.statusindicator,“,new{@class=“text danger”})
@SCILabelFor(model=>model.StatusInfo,新的{@class=“col-md-1控制标签”})
@Html.TextAreaFor(model=>model.StatusInfo,新的{@class=“form control”})
@Html.ValidationMessageFor(model=>model.StatusInfo,“,new{@class=“text danger”})
@SCILabelFor(model=>model.StageCompletion,新的{@class=“col-md-1控制标签”})
@Html.SCINumericTextBoxFor(model=>model.StageCompletion,新的{@class=“form control sliderangementext”,@style=“width:100px”})
@SCILabelFor(model=>model.IsMandatory,新的{@class=“col-md-1控制标签”})
@Html.DropDownListFor(model=>model.IsMandatory,new List(){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”})
}
这样,只有一个视图。您似乎应该使用模式模型工厂。 使用属性和方法创建接口IStage(仅声明)。然后所有的类D*阶段都实现了IStage。因此,视图具有作为模型的IStage。 通过这种方式,每个类可以在每个方法中做不同的事情
interface IStage{
byte StatusIndicatorId { get; set; }
string StatusInfo { get; set; }
decimal StageCompletion { get; set; }
bool IsMandatory { get; set; }
}
public partial class D1Stage : EntityBase, IStage
{
[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; }
}
@看法
@model-IStage
@{
IEnumerable StatusIndicators=ViewBag.StatusIndicators;
}
@使用(Html.BeginForm(“,”,FormMethod.Post,new{enctype=“multipart/formdata”,
@class=“form horizontal”})
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model=>model.Id)
@Html.HiddenFor(model=>model.Report.Id)
@SCILabelFor(model=>model.statusindicator,新的{@class=“col-md-1控制标签”})
@Html.DropDownListFor(model=>model.statusindicator,new SelectList(StatusIndicators,“Value”,“Text”),“None”,new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.statusindicator,“,new{@class=“text danger”})
@SCILabelFor(model=>model.StatusInfo,新的{@class=“col-md-1控制标签”})
@Html.TextAreaFor(model=>model.StatusInfo,新的{@class=“form control”})
@Html.ValidationMessageFor(model=>model.StatusInfo,“,new{@class=“text danger”})
@SCILabelFor(model=>model.StageCompletion,新的{@class=“col-md-1控制标签”})
@SCINumericTextBoxFor(model=>model.StageCompletion,new{@class=“form contr