简体   繁体   English

实体框架中的mvc插入不能为null错误

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

I am getting an exception that says the user email cannot be null. 我收到一个例外消息,说用户电子邮件不能为null。 The User.Identity.Name; User.Identity.Name; is set to the users email address however when I try to create a new post the exception is thrown saying: 设置为用户的电子邮件地址,但是当我尝试创建新帖子时,抛出异常:

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.

Im not sure how to add the users email address using the User.Identity.Name; 我不确定如何使用User.Identity.Name;添加用户的电子邮件地址User.Identity.Name; to the Post create method. 到后期创建方法。 It works fine in my authentication method using User.Identity.Name; 在使用User.Identity.Name;身份验证方法中,它工作正常User.Identity.Name; . However I created some extra tables in Entity Framework 5 and in the Post table I have the BlogUserEmail as a Foreign Key: 但是,我在Entity Framework 5中创建了一些额外的表,在Post表中,我将BlogUserEmail作为外键:

![enter image description here][1] ![在此处输入图片描述] [1]

View: 视图:

@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>

Since you are picking this up from User.Identity.Name, you can assign it in the HttpPost method. 由于您是从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;
    }

}

Since it is not an editable field in the Create View, you can also take an approach of putting in a HiddenField. 由于它不是“创建视图”中的可编辑字段,因此,您也可以采用放置HiddenField的方法。

Remember that readonly labels will not bind to your model in the Post method. 请记住,在Post方法中,只读标签不会绑定到您的模型。

If the email can be change in the post form, maybe not now, but in time, you can add it as hidden field with a default value. 如果可以更改帖子形式的电子邮件(也许不是现在),但是可以及时更改,则可以将其添加为具有默认值的隐藏字段。

@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