0

创建新学生时,它现在不再需要执行 db.SaveChanges() 给出验证错误:需要 Student_Nummer。即使该字段是自动增量字段。

控制器:

[HttpPost]
public ActionResult Create(Tbl_Student tbl_student)
{
    if (ModelState.IsValid)
    {
        try 
        {
            db.Tbl_Student.Add(tbl_student);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        catch (DbEntityValidationException dbEx)
        {
            foreach (var validationErrors in dbEx.EntityValidationErrors)
            {
                foreach (var validationError in validationErrors.ValidationErrors)
                {
                    Trace.TraceInformation("Property: {0} Error: {1}",
                        validationError.PropertyName, validationError.ErrorMessage);
                }
            }
        }

    }

    return View(tbl_student);
}

风景:

@model PVBMVC.Tbl_Student

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

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(false)

    <fieldset>
        <legend>Tbl_Student</legend>

        @Html.HiddenFor(model => model.Student_Nummer)

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_Id)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_Id)
            @Html.ValidationMessageFor(model => model.Student_Id)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_Roepnaam)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_Roepnaam)
            @Html.ValidationMessageFor(model => model.Student_Roepnaam)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_TussenV)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_TussenV)
            @Html.ValidationMessageFor(model => model.Student_TussenV)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_Achternaam)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_Achternaam)
            @Html.ValidationMessageFor(model => model.Student_Achternaam)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_Adres)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_Adres)
            @Html.ValidationMessageFor(model => model.Student_Adres)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_Postcode)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_Postcode)
            @Html.ValidationMessageFor(model => model.Student_Postcode)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_Woonplaats)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_Woonplaats)
            @Html.ValidationMessageFor(model => model.Student_Woonplaats)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_Telefoon)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_Telefoon)
            @Html.ValidationMessageFor(model => model.Student_Telefoon)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_GeboorteDatum)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_GeboorteDatum)
            @Html.ValidationMessageFor(model => model.Student_GeboorteDatum)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_Geslacht)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_Geslacht)
            @Html.ValidationMessageFor(model => model.Student_Geslacht)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_TelefoonNR1)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_TelefoonNR1)
            @Html.ValidationMessageFor(model => model.Student_TelefoonNR1)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_TelefoonNR2)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_TelefoonNR2)
            @Html.ValidationMessageFor(model => model.Student_TelefoonNR2)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_GeboortePlaats)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_GeboortePlaats)
            @Html.ValidationMessageFor(model => model.Student_GeboortePlaats)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_Opleiding)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_Opleiding)
            @Html.ValidationMessageFor(model => model.Student_Opleiding)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_Leerplicht)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_Leerplicht)
            @Html.ValidationMessageFor(model => model.Student_Leerplicht)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_VorigeSchool)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_VorigeSchool)
            @Html.ValidationMessageFor(model => model.Student_VorigeSchool)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_Email)
            @Html.ValidationMessageFor(model => model.Student_Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Klas_Id)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Klas_Id)
            @Html.ValidationMessageFor(model => model.Klas_Id)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Student_Wachtwoord)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Student_Wachtwoord)
            @Html.ValidationMessageFor(model => model.Student_Wachtwoord)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
4

0 回答 0