我无法创建一个能够向2个表添加数据的MVC视图

时间:2019-06-03 09:33:20

标签: c# sql-server asp.net-mvc razor

我正在尝试创建一个能够向2个表(SeriesData和SeasonsNEpisodes)添加数据的视图。我的目标是在SeriesData中添加一个系列,然后添加本赛季所拥有的季节和情节的数量。

我能够创建一个能够添加1个表的数据的视图,但我也想添加SeasonsNEpisodes表。

第一个表的代码是这样的:

public ActionResult Create(SeriesData seriesData)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.SeriesData.Add(seriesData);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(seriesData);
            }
            catch
            {
                return View();
            }

这是视图(mvc附带的创建视图):

@model WebApplication3.Models.SeriesData

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Create</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>SeriesData</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.SerieID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.SerieID, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SerieID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.SerieName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.SerieName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SerieName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.SerieCategory, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.SerieCategory, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SerieCategory, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.SerieDescription, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.SerieDescription, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SerieDescription, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.SerieYear, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.SerieYear, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SerieYear, "", 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>

我当时正在考虑创建指向另一个页面的超链接并在其中做,但是这次我想做不同的事情。我需要重新尝试吗?

编辑数据库:

Series table:
SeriesData (
SeriesID int PK,
SeriesName varchar(50),
SeriesDescription text,
SeriesCategory varchar(50),
SeriesYear varchar(4));

带有季节和情节的表格:

SeasonsEpisodes (
SeasonsEpisodesId in PK,
SeriesID int FK,
SerieSeasons int,
SerieEpisodes int)

1 个答案:

答案 0 :(得分:2)

您的视图模型为@model WebApplication3.Models.SeriesDataViewModel ,但您希望视图能够具有多个模型,以实现此目的,请创建一个像以下这样的viewModel:-

Seasons

然后在您的视图中进行以下更改:-

Episodes

同样,从数据库中获取Serie实体时,您还必须获取SeriesSub Import_data() Dim wb As Workbook Dim sFound As String, WB1 As Workbook, WB2 As Workbook Set WB1 = ThisWorkbook sFound = Dir(ActiveWorkbook.path & "\*Name.xlsx") 'the first one found If sFound <> "" Then Workbooks.Open Filename:=ActiveWorkbook.path & "\" & sFound Set WB2 = ActiveWorkbook End If WB2.Worksheets("Sheet2").Range("A5").Copy _ WB1.Worksheets("Sheet2").Range("K18") End Sub 以及if(z&S&&!ab&K==INF&d>2&v>V&v<Beta){