Diogo Teixeira
Diogo Teixeira

Reputation: 91

I can't create a mvc view able to add data to 2 tables

I am trying to create a view able to add data to 2 tables (SeriesData and SeasonsNEpisodes). My goal is to add a serie to SeriesData and then add the number of seasons and episodes this season has.

I was able to create a view able to add data of 1 table, but I also wanted to add the SeasonsNEpisodes table.

The code for the first table is this:

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

            return View(seriesData);
        }
        catch
        {
            return View();
        }
}

and this is the view (the create view that comes with 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>

I was thinking about creating a hyperlink to another page and do it in there, but I want to do it different this time. Do I have to create another try or something?

EDIT databases:

Series table:

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

Table with seasons and episodes:

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

Upvotes: 1

Views: 127

Answers (1)

Fakhar Ahmad Rasul
Fakhar Ahmad Rasul

Reputation: 1691

your view's model is SeriesData but you want your view to able to have multiple models, to achieve this create a viewModel like:-

public class SeriesDataViewModel
{
    //series data properties

    public List<Season> Seasons { get; set; }
    public List<Episode> Episodes { get; set; }
}

then make the following changes in your view:-

@model WebApplication3.Models.SeriesDataViewModel

Also when getting the Serie entity from database you have to get Seasons and Episodes along with the Series

Upvotes: 2

Related Questions