C# 使用外键创建ViewModel
我只是一个MVC的初学者,所以请帮我解决这个问题。我有一个场景,但我不知道如何为此创建适当的ViewModel。我有一个ProjectViewModel,它由4个相互关联的表组成 到目前为止,这是我的ProjectViewModelC# 使用外键创建ViewModel,c#,asp.net-mvc,asp.net-mvc-5,asp.net-mvc-viewmodel,C#,Asp.net Mvc,Asp.net Mvc 5,Asp.net Mvc Viewmodel,我只是一个MVC的初学者,所以请帮我解决这个问题。我有一个场景,但我不知道如何为此创建适当的ViewModel。我有一个ProjectViewModel,它由4个相互关联的表组成 到目前为止,这是我的ProjectViewModel public class ProjectViewModel { public int? ProjectId { get; set; } public string ProjectName { get; set; } p
public class ProjectViewModel
{
public int? ProjectId { get; set; }
public string ProjectName { get; set; }
public string ProjectLocation { get; set; }
public string ProjectDescription { get; set; }
public double? WorkArea { get; set; }
public string ModeOfPayment { get; set; }
public string Duration { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public double? TotalDirectCost { get; set; }
public double? ProfitSupervision { get; set; }
public double? TotalProjectCost { get; set; }
public List<ScopeOfWork> ScopeOfWork { get; set; }
}
public class ScopeOfWork
{
public int? ScopeOfWorkId { get; set; }
public string ScopeOfWorkName { get; set; }
public List<Materials> Materials { get; set; }
public int? ProjectId { get; set; }
}
public class Materials
{
public string MaterialName { get; set; }
public int? Quantity { get; set; }
public double? Cost { get; set; }
public int? ScopeOfWorkId { get; set; }
//This should be a select list
public IEnumerable<SelectListItem> Category { get; set;
}
视图:
@model MigratingDB.Models.ProjectViewModel
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
项目视图模型
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.ProjectId,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.ProjectId,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.ProjectId,“,new{@class=“text danger”})
@LabelFor(model=>model.ProjectName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.ProjectName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.ProjectName,“,new{@class=“text danger”})
@LabelFor(model=>model.ProjectLocation,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.ProjectLocation,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.ProjectLocation,“,new{@class=“text danger”})
@LabelFor(model=>model.ProjectDescription,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.ProjectDescription,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.ProjectDescription,“,new{@class=“text danger”})
@LabelFor(model=>model.WorkArea,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.WorkArea,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.WorkArea,“,new{@class=“text danger”})
@LabelFor(model=>model.ModeOfPayment,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.ModeOfPayment,new{htmlAttributes=new{@class=“formcontrol”}})
@Html.ValidationMessageFor(model=>model.ModeOfPayment,“,new{@class=“text danger”})
@LabelFor(model=>model.Duration,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Duration,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Duration,“,new{@class=“text danger”})
@LabelFor(model=>model.StartDate,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.StartDate,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.StartDate,“,new{@class=“text danger”})
@LabelFor(model=>model.EndDate,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.EndDate,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.EndDate,“,new{@class=“text danger”})
@LabelFor(model=>model.TotalDirectCost,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.TotalDirectCost,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.TotalDirectCost,“,new{@class=“text danger”})
@LabelFor(model=>model.profitspervision,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.profitspervision,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.profitspervision,“,new{@class=“text danger”})
@LabelFor(model=>model.TotalProjectCost,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.TotalProjectCost,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.TotalProjectCost,“,new{@class=“text danger”})
@LabelFor(model=>model.ProjectStatus,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.ProjectStatus,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.ProjectStatus,“,new{@class=“text danger”})
}
@ActionLink(“返回列表”、“索引”)
请给我一些建议,以便为此设置一个正确的ViewModel,这将允许我创建具有多个工作范围的项目,并为每个创建的工作范围创建相应的材质
更新:我将每个ViewModel中的类分开
[HttpGet]
public ActionResult _CreateProject()
{
return View();
}
//POST: Create Project
[HttpPost]
public ActionResult _CreateProject(ProjectViewModel project)
{
return View(project);
}
@model MigratingDB.Models.ProjectViewModel
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>ProjectViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ProjectId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProjectId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProjectId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProjectName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProjectName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProjectName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProjectLocation, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProjectLocation, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProjectLocation, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProjectDescription, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProjectDescription, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProjectDescription, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.WorkArea, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.WorkArea, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.WorkArea, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ModeOfPayment, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ModeOfPayment, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ModeOfPayment, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Duration, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Duration, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Duration, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EndDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TotalDirectCost, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TotalDirectCost, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TotalDirectCost, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProfitSupervision, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProfitSupervision, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProfitSupervision, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TotalProjectCost, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TotalProjectCost, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TotalProjectCost, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProjectStatus, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProjectStatus, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProjectStatus, "", new { @class = "text-danger" })
</div>
</div>
<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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
public class ProjectViewModel
{
//tbl Project
public int? ProjectId { get; set; }
public string ProjectName { get; set; }
public string ProjectLocation { get; set; }
public string ProjectDescription { get; set; }
public double? WorkArea { get; set; }
public string ModeOfPayment { get; set; }
public string Duration { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public double? TotalDirectCost { get; set; }
public double? ProfitSupervision { get; set; }
public double? TotalProjectCost { get; set; }
//SelectList
public int? ProjectStatus { get; set; }
//SelectList
public int? ForemanId { get; set; }
//SelectList
public int? ClientId { get; set; }
//tbl ScopeOfWork
public List<ScopeOfWorkViewModel> ScopeOfWork { get; set; }
}
public class ScopeOfWorkViewModel
{
public int? ScopeOfWorkId { get; set; }
public string ScopeOfWorkName { get; set; }
public List<MaterialsViewModel> Materials { get; set; }
public int? ProjectId { get; set; }
}
public class MaterialsViewModel
{
public string MaterialName { get; set; }
public int? Quantity { get; set; }
public double? Cost { get; set; }
public int? ScopeOfWorkId { get; set; }
public IEnumerable<SelectListItem> Category { get; set; }
}