插入1个父行和许多子行Asp.net MVC

时间:2016-04-07 14:45:06

标签: asp.net-mvc

所以我正在创建一个系统,您可以在其中创建表单并将其存储在数据库中,该数据库将显示特定类型的报告。

在报告中,您将在报告表格中输入1行,其中包含报告所有者,提交日期,状态等默认报告数据

在同一个创建页面上,我希望将所创建的表单中的所有值存储在数据库中,到目前为止,我已设法使显示的表单正确显示在报表中。

DB中表单的每个输入都是ReportValues表中的一个输入。

目前我可以从“创建视图”获取报告,但ReportValue的条目不会返回到“创建Iaction”。

请帮忙:/

型号:

public class Report
{
    public Report()
    {
        //Set default Submission Date to the current date.
        this.SubmissionDate = DateTime.Now;
        this.Status = "New";
    }

    public int ID { get; set; }

    [ForeignKey("Form")]
    public int formID { get; set; }

    [StringLength(5, MinimumLength = 3)]
    public String Abreviation { get; set; }

    [Display(Name = "Submitted By")]
    public String Owner { get; set; }


    [RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$")]
    public String Category { get; set; }

    public String Status { get; set; }

    [DataType(DataType.Date), Display(Name = "Submit Date")]
    public DateTime SubmissionDate { get; set; }

    }
}

    public class ReportValues
{
    public int ID { get; set; }
    [ForeignKey("Report")]
    public int reportId { get; set; }
    public String title { get; set; }
    public String value { get; set; }
}
}

    public class FormReportViewModel
{
    public Report Report { get; set; }
    public int reportID { get; set; }
    public String formCode { get; set; }
    public String abv { get; set; }
    public String category { get; set; }
    public IEnumerable<ReportValues> ReportValues { get; set; }
}
}

创建Iaction:

        // POST: Reports/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Create(FormReportViewModel formReportViewModel)
    {
        Report r = formReportViewModel.Report;
        if (ModelState.IsValid)
        {
            _context.Report.Add(r);
            _context.SaveChanges();
            foreach (var item in formReportViewModel.ReportValues)
            {
                ReportValues rv = new ReportValues { reportId = r.ID, title = item.title, value = item.value };
                _context.ReportValues.Add(rv);
                _context.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        return View(formReportViewModel);
    }

查看:

model IEnumerable<FormReportViewModel>
@using System.Security.Claims;

@{
ViewData["Title"] = "Create";
}

<h2>Create</h2>
<form asp-action="Create">
    <div class="form-horizontal">
        <h4>Report</h4>
        <hr />
        <div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="@Model.First().Report.Abreviation" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="@Model.First().Report.Abreviation" class="form-control" value="@Model.First().abv" readonly="readonly"/>
                <span asp-validation-for="@Model.First().Report.Abreviation" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <label asp-for="@Model.First().Report.Category" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="@Model.First().Report.Category" class="form-control" value="@Model.First().category" readonly="readonly"/>
                <span asp-validation-for="@Model.First().Report.Category" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <label asp-for="@Model.First().Report.Owner" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="@Model.First().Report.Owner" class="form-control" value=@User.GetUserName() readonly="readonly"/>
                <span asp-validation-for="@Model.First().Report.Owner" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <label asp-for="@Model.First().Report.Status" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="@Model.First().Report.Status" class="form-control" value="New" readonly="readonly"/>
                <span asp-validation-for="@Model.First().Report.Status" class="text-danger" />
            </div>
        </div>
       @*Html.Raw(Model.First().formCode)*@
       @*
            The code below is from the Create ReportValues page    
       *@
        <div class="form-group">
            <label class="col-md-2 control-label" for="title">title</label>
            <div class="col-md-10">
                <input class="form-control" type="text" id="title" name="title" value="Animal Name" />
                <span class="text-danger field-validation-valid" data-valmsg-for="title" data-valmsg-replace="true" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-md-2 control-label" for="value">value</label>
            <div class="col-md-10">
                <input class="form-control" type="text" id="value" name="value" value="" />
                <span class="text-danger field-validation-valid" data-valmsg-for="value" data-valmsg-replace="true" />
            </div>
        </div>

        <div class="form-group">
            <label class="col-md-2 control-label" for="title">title</label>
            <div class="col-md-10">
                <input class="form-control" type="text" id="title" name="title" value="Animal Weight" />
                <span class="text-danger field-validation-valid" data-valmsg-for="title" data-valmsg-replace="true" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-md-2 control-label" for="value">value</label>
            <div class="col-md-10">
                <input class="form-control" type="text" id="value" name="value" value="" />
                <span class="text-danger field-validation-valid" data-valmsg-for="value" data-valmsg-replace="true" />
            </div>
        </div>

        <div class="form-group">
            <label class="col-md-2 control-label" for="title">title</label>
            <div class="col-md-10">
                <input class="form-control" type="text" id="title" name="title" value="Animal Colour" />
                <span class="text-danger field-validation-valid" data-valmsg-for="title" data-valmsg-replace="true" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-md-2 control-label" for="value">value</label>
            <div class="col-md-10">
                <input class="form-control" type="text" id="value" name="value" value="" />
                <span class="text-danger field-validation-valid" data-valmsg-for="value" data-valmsg-replace="true" />
            </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>
</form>




<div>
    <a asp-action="Index">Back to List</a>
</div>

0 个答案:

没有答案