在MVC4中自定义CRUD时出现数据库错误

时间:2016-03-05 10:57:00

标签: c# angularjs asp.net-mvc

  

INSERT语句与FOREIGN KEY约束冲突   " FK_tblCompInfo_tblOSVersion&#34 ;.冲突发生在数据库中   "示例",表" dbo.tblOSVersion",列' VerID'。声明有   已被终止。

{
   db.CompInfoes.Add(compinfo);      
   db.SaveChanges();
   return RedirectToAction("Index");   
}

尝试使用以下代码自定义Create.cshtml

@model MvcApplication1.Models.CompInfo  
@{
    ViewBag.Title = "Create";
}
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/MyApp.js"></script>
<script src="~/Scripts/DropDownController.js"></script>
<h2>Create</h2>
<div style="font-family:Arial">
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>CompInfo</legend>

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

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

        <div class="editor-label">
            @Html.LabelFor(model => model.ServiceTag)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ServiceTag)
            @Html.ValidationMessageFor(model => model.ServiceTag)
        </div>
        <div class="editor-label">
        <div ng-app="MyApp" ng-controller="DropDownController">
                Country : <select ng-model="OSID" ng-options="I.OSID as I.OSName for I in OSList" ng-change="GetVersion()">
                <option value="">Select OS</option>
                </select><br />
                State : <select ng-model="VerID" ng-options="I.VerID as I.VerName for I in VersionList">
                <option value="">{{VersionTextToShow}}</option>
            </select>
    </div>
</div>

        @*<div class="editor-label">
            @Html.LabelFor(model => model.OSID, "OperatingSystem")
        </div>
        <div class="editor-field">
            @Html.DropDownList("OSID", String.Empty)
            @Html.ValidationMessageFor(model => model.OSID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.VerID, "OSVersion")
        </div>
        <div class="editor-field">
            @Html.DropDownList("VerID", String.Empty)
            @Html.ValidationMessageFor(model => model.VerID)
        </div>*@

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

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

        <div class="editor-label">
            @Html.LabelFor(model => model.SysTypeID, "SystemType")
        </div>
        <div class="editor-field">
            @Html.DropDownList("SysTypeID", String.Empty)
            @Html.ValidationMessageFor(model => model.SysTypeID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CStatusID, "StatusComputer")
        </div>
        <div class="editor-field">
            @Html.DropDownList("CStatusID", String.Empty)
            @Html.ValidationMessageFor(model => model.CStatusID)
        </div>

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

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
</div>

1 个答案:

答案 0 :(得分:0)

错误表示数据不受外键列的影响,或映射表不支持数据。 可以有两个选项

  

1.如果外键的数据应为null。应改变外键列以允许null。

     

2.如果一对多映射是正确的,只有映射表数据将被正确地用&#34; db.CompInfoes.Add(compinfo);&#34;