使用外键创建ViewModel

时间:2017-06-06 02:47:38

标签: 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; }
        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; 
}

假设一个Project可以有很多ScopeOfWork对象,而ScopeOfWork对象可以有很多物质对象。我将填充控制器中类别列表的类别属性。

我只是尝试为List ScopeOfWork Object创建一个视图,以查看ScopeOfWork对象是否会显示但未在视图中显示。

控制器:

 [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>

请提供建议为此设置正确的ViewModel的建议,这将允许我创建具有多个ScopeOfWork的项目及其创建的每个ScopeOfWork的相应材料。

更新:我将每个ViewModel中的类分开

ProjectViewModel

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; }
        }

ScopeOfWorkViewModel

 public class ScopeOfWorkViewModel
    {
        public int? ScopeOfWorkId { get; set; }
        public string ScopeOfWorkName { get; set; }
        public List<MaterialsViewModel> Materials { get; set; }
        public int? ProjectId { get; set; }
    }

MaterialsViewModel

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; }
}

0 个答案:

没有答案