簡體   English   中英

實體框架中的mvc插入不能為null錯誤

[英]mvc insert cannot be null error in entity framework

我收到一個例外消息,說用戶電子郵件不能為null。 User.Identity.Name; 設置為用戶的電子郵件地址,但是當我嘗試創建新帖子時,拋出異常:

Cannot insert the value NULL into column 'BlogUserEmail', table
'Blog.dbo.Posts'; column does not allow nulls. INSERT fails.\r\nThe
statement has been terminated.

我不確定如何使用User.Identity.Name;添加用戶的電子郵件地址User.Identity.Name; 到后期創建方法。 在使用User.Identity.Name;身份驗證方法中,它工作正常User.Identity.Name; 但是,我在Entity Framework 5中創建了一些額外的表,在Post表中,我將BlogUserEmail作為外鍵:

![在此處輸入圖片描述] [1]

視圖:

@model MyBlogger.Post

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

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Post</legend>

        <div class="editor-label"> // I changed this area
            @Html.LabelFor(model => model.BlogUserEmail, User.Identity.Name)
        </div>
        <div class="editor-field">
            @Html.ValidationMessageFor(model => model.BlogUserEmail)
        </div>

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

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

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

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

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

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


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

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

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

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

由於您是從User.Identity.Name進行選擇的,因此可以在HttpPost方法中進行分配。

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Post post)
{
    post.BlogUserEmail = User.Identity.Name
    try
    {
        if (ModelState.IsValid)
        {
            db.Posts.Add(post);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        var email = User.Identity.Name;
        ViewBag.BlogUserEmail = new SelectList(db.BlogUsers, email);
        ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", post.CategoryId);
        return View(post);
    }
    catch (DbEntityValidationException e)
    {
        var newException = new FormattedDbEntityValidationException(e);
        throw newException;
    }

}

由於它不是“創建視圖”中的可編輯字段,因此,您也可以采用放置HiddenField的方法。

請記住,在Post方法中,只讀標簽不會綁定到您的模型。

如果可以更改帖子形式的電子郵件(也許不是現在),但是可以及時更改,則可以將其添加為具有默認值的隱藏字段。

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Post</legend>

    <div class="editor-label"> // I changed this area
        @Html.LabelFor(model => model.BlogUserEmail, User.Identity.Name)
    </div>
    <div class="editor-field">
        @Html.ValidationMessageFor(model => model.BlogUserEmail)
    </div>
    @* Hidden Field *@
    @Html.HiddenFor(model => model.BlogUserEmail,new { value = User.Identity.Name })

    // rest of the html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM