我有一个相对简单的流程:
发布项目 - >发布预览 - >选项A:编辑|选项B:发布
一切正常,尽可能使用标准CRUD。但是,在发布项目的情况下,然后进入预览屏幕并且用户想要发布(这实际上只是在数据库中将bool从false更改为true)我收到以下错误消息:
一个或多个实体的验证失败。看到 'EntityValidationErrors'属性以获取更多详细信息。描述:一个 在执行当前Web期间发生了未处理的异常 请求。请查看堆栈跟踪以获取有关的更多信息 错误以及它在代码中的起源。
异常详细信息: System.Data.Entity.Validation.DbEntityValidationException:验证 一个或多个实体失败。请参阅'EntityValidationErrors'属性 了解更多详情。
与标准CRUD一样,我在编辑时调用项目的ID,但是当从创建移动到预览时,似乎没有逻辑方法来调用ID,因为它刚刚被创建。但是,通过介于两者之间的额外步骤,我认为问题在于我尝试编辑时没有在预览视图中指定ID。
这是发布部分的控制器代码:
//POST: /errandom/errandPreview
public ActionResult ConfirmErrand()
{
// define variables for reuse
var userID = User.Identity.GetUserId();
DateTime nowUTC = DateTime.Now.ToUniversalTime();
DateTime nowLocal = DateTime.Now.ToLocalTime();
// define errand
var errand = new errands
{
// form
Published = true
};
// save errand to DB
ERRANDOM.Entry(errand).State = EntityState.Modified;
ERRANDOM.SaveChanges();
return RedirectToAction("Index");
}
加上完整性的观点:
<section id="errandPreview" class="manageForm">
@using (Html.BeginForm("errandPreviewForm", "errandom", FormMethod.Post, new { @id = "errandPreviewForm", @class = "form-horizontal", @role = "form" }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.LatitudePosted, new { @id = "errandPreviewLatitudePosted", @class = "userActivity" })
@Html.HiddenFor(m => m.LongitudePosted, new { @id = "errandPreviewLongitudePosted", @class = "userActivity" })
@Html.HiddenFor(m => m.LocationPosted, new { @id = "errandPreviewUserLocationPosted", @class = "userActivity" })
@Html.HiddenFor(m => m.ID, new { @id = "errandPreviewID", @class = "errandPreviewID" })
<div id="errandPreviewErrandSection" class="manageSection">
<p id="errandPreviewErrandSectionTitle" class="manageSectionTitle">
@Html.DisplayFor(m => m.Title)
</p>
<div id="errandPreviewCategoryArea" class="manageArea row">
<div id="errandPreviewDateTimePostedDisplayName" class="errandomDisplayName col-xs-offset-1 col-xs-10 col-sm-offset-1 col-sm-10 col-md-offset-1 col-md-10 col-lg-offset-1 col-lg-10">
@Html.DisplayNameFor(m => m.DateTimePosted)
</div>
<div id="errandPreviewDateTimePostedDisplay" class="errandomDisplay col-xs-offset-1 col-xs-10 col-sm-offset-1 col-sm-10 col-md-offset-1 col-md-10 col-lg-offset-1 col-lg-10">
@Html.DisplayFor(m => m.DateTimePosted)
</div>
<div id="errandPreviewCategoryDisplayName" class="errandomDisplayName col-xs-offset-1 col-xs-5 col-sm-offset-1 col-sm-5 col-md-offset-1 col-md-5 col-lg-offset-1 col-lg-5">
@Html.DisplayNameFor(m => m.Category)
</div>
<div id="errandPreviewSubCategoryDisplayName" class="errandomDisplayName col-xs-5 col-sm-5 col-md-5 col-lg-5">
@Html.DisplayNameFor(m => m.SubCategory)
</div>
<div id="errandPreviewCategoryDisplay" class="errandomDisplay col-xs-offset-1 col-xs-5 col-sm-offset-1 col-sm-5 col-md-offset-1 col-md-5 col-lg-offset-1 col-lg-5">
@Html.DisplayFor(m => m.Category)
</div>
<div id="errandPreviewSubCategoryDisplay" class="errandomDisplay col-xs-5 col-sm-5 col-md-5 col-lg-5">
@Html.DisplayFor(m => m.SubCategory)
</div>
</div>
<hr />
<div id="errandPreviewDescriptionArea" class="manageArea row">
<div id="errandPreviewPictureDisplay" class="errandomDisplay col-xs-offset-1 col-xs-10 col-sm-offset-1 col-sm-3 col-md-offset-1 col-md-3 col-lg-offset-1 col-lg-3">
<img id="errandPreviewPicture" class="errandPreviewPicture" src="@Url.Action("errandPicture", "errandom")" />
</div>
<div id="errandPreviewDescriptionDisplay" class="errandomDisplay col-xs-offset-1 col-xs-10 col-sm-offset-0 col-sm-7 col-md-offset-0 col-md-7 col-lg-offset-0 col-lg-7">
@Html.DisplayFor(m => m.Description)
</div>
</div>
<hr />
<div id="errandPreviewLocationArea" class="manageArea row">
<div id="errandPreviewLocationDisplayName" class="errandomDisplayName col-xs-offset-1 col-xs-10 col-sm-offset-1 col-sm-10 col-md-offset-1 col-md-10 col-lg-offset-1 col-lg-10">
@Html.DisplayNameFor(m => m.Location)
</div>
<div id="errandPreviewLocationDisplay" class="errandomDisplay col-xs-offset-1 col-xs-10 col-sm-offset-1 col-sm-10 col-md-offset-1 col-md-10 col-lg-offset-1 col-lg-10">
@Html.DisplayFor(m => m.Location)
@Html.HiddenFor(m => m.Location, new { @id = "errandPreviewLocationDisplayValue", @class = "manageDisplay" })
</div>
<div id="errandPreviewMapDisplayFrame" class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div id="errandPreviewMapDisplay" class="errandomDisplay col-xs-offset-1 col-xs-10 col-sm-offset-1 col-sm-10 col-md-offset-1 col-md-10 col-lg-offset-1 col-lg-10">
</div>
</div>
</div>
<hr />
<div id="errandPreviewTimeArea" class="manageArea row">
<div id="errandPreviewASAPDisplayName" class="errandomDisplayName col-xs-offset-1 col-xs-5 col-sm-offset-1 col-sm-5 col-md-offset-1 col-md-5 col-lg-offset-1 col-lg-5">
@Html.DisplayNameFor(m => m.ASAP)
</div>
<div id="errandPreviewASAPDisplay" class="errandomDisplay col-xs-offset-0 col-xs-5 col-sm-offset-0 col-sm-5 col-md-offset-0 col-md-5 col-lg-offset-0 col-lg-5">
@Html.DisplayFor(m => m.ASAP)
</div>
<div id="errandPreviewStartDateTimeDisplayName" class="errandomDisplayName col-xs-offset-1 col-xs-10 col-sm-offset-1 col-sm-5 col-md-offset-1 col-md-5 col-lg-offset-1 col-lg-5">
@Html.DisplayNameFor(m => m.StartDateTime)
</div>
<div id="errandPreviewStartDateTimeDisplay" class="errandomDisplay col-xs-offset-1 col-xs-10 col-sm-offset-0 col-sm-5 col-md-offset-0 col-md-5 col-lg-offset-0 col-lg-5">
@Html.DisplayFor(m => m.StartDateTime)
</div>
<div id="errandPreviewEndDateTimeDisplayName" class="errandomDisplayName col-xs-offset-1 col-xs-10 col-sm-offset-1 col-sm-5 col-md-offset-1 col-md-5 col-lg-offset-1 col-lg-5">
@Html.DisplayNameFor(m => m.EndDateTime)
</div>
<div id="errandPreviewEndDateTimeDisplay" class="errandomDisplay col-xs-offset-1 col-xs-10 col-sm-offset-0 col-sm-5 col-md-offset-0 col-md-5 col-lg-offset-0 col-lg-5">
@Html.DisplayFor(m => m.EndDateTime)
</div>
<div id="errandPreviewDurationInHoursDisplayName" class="errandomDisplayName col-xs-offset-1 col-xs-5 col-sm-offset-1 col-sm-5 col-md-offset-1 col-md-5 col-lg-offset-1 col-lg-5">
@Html.DisplayNameFor(m => m.DurationInHours)
</div>
<div id="errandPreviewDurationInHoursDisplay" class="errandomDisplay col-xs-offset-0 col-xs-5 col-sm-offset-0 col-sm-5 col-md-offset-0 col-md-5 col-lg-offset-0 col-lg-5">
@Html.DisplayFor(m => m.DurationInHours)
</div>
</div>
<hr />
<div id="errandPreviewOfferingArea" class="manageArea row">
<div id="errandPreviewOfferingDisplayName" class="errandomDisplayName col-xs-offset-1 col-xs-5 col-sm-offset-1 col-sm-5 col-md-offset-1 col-md-5 col-lg-offset-1 col-lg-5">
@Html.DisplayNameFor(m => m.Offering)
</div>
<div id="errandPreviewPaymentMethodDisplayName" class="errandomDisplayName col-xs-5 col-sm-5 col-md-5 col-lg-5">
@Html.DisplayNameFor(m => m.PaymentMethod)
</div>
<div id="errandPreviewCurrencyDisplay" class="errandomDisplay col-xs-offset-1 col-xs-2 col-sm-offset-1 col-sm-1 col-md-offset-1 col-md-1 col-lg-offset-1 col-lg-1">
@Html.DisplayFor(m => m.Currency)
</div>
<div id="errandPreviewOfferingDisplay" class="errandomDisplay col-xs-offset-0 col-xs-3 col-sm-offset-0 col-sm-4 col-md-offset-0 col-md-4 col-lg-offset-0 col-lg-4">
@Html.DisplayFor(m => m.Offering)
</div>
<div id="errandPreviewPaymentMethodDisplay" class="errandomDisplay col-xs-5 col-sm-5 col-md-5 col-lg-5">
@Html.DisplayFor(m => m.PaymentMethod)
</div>
<div id="errandPreviewPriceDisplayName" class="errandomDisplayName col-xs-offset-1 col-xs-12 col-sm-offset-1 col-sm-12 col-md-offset-1 col-md-12 col-lg-offset-1 col-lg-12">
@Html.DisplayNameFor(m => m.Price)
</div>
<div id="errandPreviewPriceDisplay" class="errandomDisplay col-xs-offset-1 col-xs-12 col-sm-offset-1 col-sm-12 col-md-offset-1 col-md-12 col-lg-offset-1 col-lg-12">
@Html.DisplayFor(m => m.Price)
</div>
</div>
<hr />
<div id="errandPreviewUserArea" class="manageArea row">
<div id="errandPreviewUserPictureDisplay" class="errandomDisplay col-xs-offset-1 col-xs-3 col-sm-offset-1 col-sm-2 col-md-offset-1 col-md-2 col-lg-offset-1 col-lg-2">
<img id="errandPreviewUserPicture" class="errandPreviewUserPicture" src="@Url.Action("UserPicture", "Manage")" />
</div>
<div id="errandPreviewFirstNameDisplay" class="errandomDisplay col-xs-7 col-sm-8 col-md-8 col-lg-8">
@Html.DisplayFor(m => m.FirstName)
@*link to contact for questions*@
@Html.DisplayFor(m => m.Rating)
</div>
</div>
</div>
<div id="errandPreviewButtonSection" class="manageSection">
<div id="errandPreviewButtonArea" class="manageArea row">
@Html.ActionLink("Edit", "EditErrand", "errandom", new { ID = Model.ID }, htmlAttributes: new { @id = "errandPreviewEditButton", @class = "manageButton col-xs-5 col-sm-5 col-md-5 col-lg-5" })
<div id="errandPreviewButtonSeparator" class="manageSeparator col-xs-2 col-sm-2 col-md-2 col-lg-2"></div>
@Html.ActionLink("Post", "ConfirmErrand", "errandom", new { ID = Model.ID }, htmlAttributes: new { @id = "errandPreviewPostButton", @class = "manageButton col-xs-5 col-sm-5 col-md-5 col-lg-5" })
</div>
</div>
}
</section>
知道我哪里错了吗?如上所述,目标应该是从 CREATE 直接转到 PREVIEW (基本上是精简版UPDATE),然后回到 INDEX 。
答案 0 :(得分:1)
据我了解您的问题,您希望在发布前进行预览,如果您已保存记录,则可以使用以下方式获取ID:
//POST: /errandom/errandPreview
public ActionResult ConfirmErrand(int? ID) //Add this parameter to get record ID
{
//Your other logic here..
var errand = ERRANDOM.errands.Find(ID); //Find your record
errand.Published = true;
//Update errand to DB
ERRANDOM.Entry(errand).State = EntityState.Modified;
ERRANDOM.SaveChanges();
return RedirectToAction("Index");
}
您可以使用查找方法获取记录,因为您在此处的View现有代码上传递了代码中的ID:
@Html.ActionLink("Post", "ConfirmErrand", "errandom", new { ID = Model.ID }, htmlAttributes: new { @id = "errandPreviewPostButton", @class = "manageButton col-xs-5 col-sm-5 col-md-5 col-lg-5" })