C# 继续提交表单时出错

C# 继续提交表单时出错,c#,html,asp.net,C#,Html,Asp.net,我不确定问题是在修改竞赛页面的表单部分,还是在竞赛控制器中。这是我在提交表格创建竞赛时遇到的错误 System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Contests_ContestTypes". The conflict occurred in database "Feebee", table "dbo.ContestTypes", col

我不确定问题是在修改竞赛页面的表单部分,还是在竞赛控制器中。这是我在提交表格创建竞赛时遇到的错误

System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Contests_ContestTypes". The conflict occurred in database "Feebee", table "dbo.ContestTypes", column 'Id'. The statement has been terminated. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues) at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) --- End of inner exception stack trace --- at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) at System.Data.Entity.Internal.InternalContext.SaveChanges() --- End of inner exception stack trace --- at System.Data.Entity.Internal.InternalContext.SaveChanges() at Feebee.WebUI.Controllers.ContestController.Modify(ContestModifyModel model) in c:\Solution\Feebee\Feebee.WebUI\Controllers\ContestController.cs:line 506 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)
以下是竞赛创建页面上表单的代码

<form method="post" action="@Url.Action("Modify", "Contest")">

                    <p class="important ml10 bold">Create a contest using the form below. After saving the contest, the next screen will allow for MULTIPLE PRIZES to be added. Reports are available for each contest IF A KEYWORD is added. </p>
                    @*<div class="curva lightbg shadow"> 
                        <h2>Step 1 - Contest Type</h2>
                        <p class="help-text ml10">Select the contest type that you would like to start.</p>
                        <div class="whitebg shadow curva">
                            <div class="field pl15"><label>Contest Type:</label> 
                                <div class="controles">
                                    @foreach (var item in Model.ContestTypes)
                                    {
                                    <text>
                                        <div>
                                            <input type="radio" name="ContestTypeId" value="@item.Id" checked="checked"/> 
                                            Shopper only - Shoppers can join your contest for free with a chance to win. Every entrant must have an account to join. Any new account will be captured and permanently connected to your business.
                                        </div>
                                    </text>
                                    }
                                        <div>
                                            Shopper & Business - Coming soon! This contest will have the ability to designate prizes for either a shopper OR a business.
                                        </div>

                                </div>
                            </div>
                            <br class="clear" />
                        </div>
                    </div>
                *@
                    <div class="curva lightbg shadow"> 
                        <h3>Step 1 - Contest Duration</h3>
                        <p class="help-text ml10">Set the important dates for your contest. The contest WILL NOT become active until prizes are added AND the contest is activated. Until a contest is activated it will be saved.</p>
                        <div class="whitebg shadow curva">
                            <div class="field pl15"><label><span class="required" title="This field is required">*</span> Start Date:</label> @Html.TextBox("StartDate", Model.StartDate.ToString("MM/dd/yyyy"), new { @class = "size-m datepicker" })
                                @Html.ValidationMessageFor(model => model.StartDate)</div>
                            <div class="field pl15"><label><span class="required" title="This field is required">*</span> End Date:</label> @Html.TextBox("EndDate", Model.EndDate.ToString("MM/dd/yyyy"), new { @class = "size-m datepicker" }) 
                                @Html.ValidationMessageFor(model => model.EndDate)</div>
                            <div class="field pl15"><label><span class="required" title="This field is required">*</span> Draw Date:</label> @Html.TextBox("DrawDate", Model.DrawDate.ToString("MM/dd/yyyy"), new { @class = "size-m datepicker" }) 
                            @Html.ValidationMessageFor(model => model.DrawDate)</div><br class="clear" />
                        </div>
                        <br class="clear" />
                    </div>

                    <div class="curva lightbg shadow"> 
                        <h3>Step 2 - Contest Name <span class="help-info-icon" onclick="showStuff('ContestsName'); showStuff('shade-infobox')"></span> </h3>
                        <p class="help-text ml10">This is the name that will be shown when Participants share the contest using the social links on your contest page, like Facebook and Twitter. You can have multiple prizes so it is best if your contest name <b>is not</b> the same as your prize.</p>
                        <div class="whitebg shadow curva">
                            <div class="field w100p pl15"><label><span class="required" title="This field is required">*</span> Social Display Name:</label>
                               <div class="w100p controles"> 
                                    @Html.TextBoxFor(model => model.ContestName, new { @class = "w80p" })
                                    @Html.ValidationMessageFor(model => model.ContestName)
                                    <div title="A character is any letter, number, space, punctuation mark, or symbol" class="foot-text pr25">Max 128 Characters.</div>
                               </div>
                            </div><br class="clear" />
                        </div>
                    </div>

                    <div class="curva lightbg shadow"> 
                        <h3>Step 3 - Owner's Message <span class="help-info-icon" onclick="showStuff('ContestOwnersMessage'); showStuff('shade-infobox')"></span> </h3>
                        <p class="help-text ml10">Write a message from your Business to the participants.</p>
                        <div class="whitebg shadow curva">
                            <div class="field w90p pl15"><label><span class="required" title="This field is required">*</span> Owner's Message:</label>
                               <div class="w100p controles"> 
                                    @Html.TextAreaFor(model => model.Description, new { @class = "tinymce" })
                                    @Html.ValidationMessageFor(model => model.Description) 
                                    <div title="A character is any letter, number, space, punctuation mark, or symbol" class="foot-text pr25">Max 1024 Characters.</div>
                               </div>
                            </div>
                            <br class="clear" />
                        </div>
                    </div>

                    <div class="curva lightbg shadow"> 
                        <h3>Step 4 - Contest Rules</h3>
                        <div class="whitebg shadow curva">
                            <div class="field w90p pl15">
                                @* If contest type is favorties*@
                                <label>The following rules are provided with each contest. </label>
                                <ol>
                                    <li>Entry is Free. </li>
                                    <li>Earn one raffle ticket for Entering Contest.</li>
                                    <li>Earn more raffle tickets for inviting others.</li>
                                    <li>Drawing will be randomly selected by system.</li>
                                    <li>More raffle tickets equals more chances to Win.</li>
                                    <li>Must fill out your <a href="@Url.Action("AccountProfile", "User")" title="Shopper Profile">Shopper Profile</a> to qualify & win. </li>
                                    <li>A Feebee Credit may be exchanged for the physical prizes listed in the event that a physical prize cannot be physically claimed. Any person can win any prize worldwide. All physical prizes may be claimed by winner but shipping costs can be the responsibility of the winner.</li>
                                    <li>Each contest is designed, supported and run by the business account attached to the contest. Feebee is not liable for any contest or prizes offered. </li>
                                </ol>
                                @* else
                                Select a Contest Type above to see the rules for it*@
                            </div><br class="clear" />
                            <div class="field w90p pl15"><label><span class="required" title="This field is required">*</span> Additional Rules: <span class="help-info-icon" onclick="showStuff('ContestAdditionalRules'); showStuff('shade-infobox')"></span></label>
                                <p class="help-text ml10">Provide additional rules for your Contest.</p>
                               <div class="w100p controles"> 
                                    @Html.TextAreaFor(model => model.AdditionalMechanics, new { @class = "tinymce" })
                                    @Html.ValidationMessageFor(model => model.AdditionalMechanics)
                                    <div title="A character is any letter, number, space, punctuation mark, or symbol" class="foot-text pr25">Max 1024 Characters.</div>
                               </div>
                            </div><br class="clear" />
                        </div>
                    </div>

                    <div class="mt15 acciones clearfix centered p10">
                        @Html.HiddenFor(model => model.Id)
                        @Html.HiddenFor(model => model.InviteCode)
                        @Html.HiddenFor(model => model.PlaceId)
                        <input type="submit" class="boton btn-blue" value="Save Contest" />

                    </div>
                </form>

听起来像是SQL与外键冲突的问题。您的模型的某些内容与该键没有正确关联。正如@Greg所提到的,您的外键约束存在问题:FK\u竞赛\u竞赛类型。看起来您正试图将引用无效竞赛类型的内容保存到竞赛表中。查看您的标记,您注释掉了表单中用于发布竞争类型的部分。我来此工作是为了解决此问题,显然,他们说当他们将套餐支付系统从ACH切换到账单支付系统时,它停止工作。我不确定这是否与此有关。