2016-11-25 5 views
0

Es ist mein erstes Mal mit EF und ich habe mit diesem Kopf mehr Kopf geschlagen, als ich muss.Kann nicht in meine Datenbank eingefügt werden

Im Grunde versuche ich einfach, einen Datensatz zur Datenbank hinzuzufügen, indem ich ein Submit-Formular in der Ansicht und eine create-Methode im Controller verwende.

Also im Grunde, ich habe ein sehr einfaches Modell und eine Steuerung mit einer Create Methode

Hier ist der Code:

Modell mit DbContext:

using System.Data.Entity; 

namespace class_project.Models 
{ 
    public class Post 
    { 
     public int PostId { get; set; } 
     [Required(ErrorMessage = "no!")] 
     public string Title { get; set; } 
     [Required(ErrorMessage = "no!")] 
     public string Content { get; set; } 

     public Post(int PostId, string Title, string Content) 
     { 
      this.PostId = PostId; 
      this.Title = Title; 
      this.Content = Content; 
     } 

     public Post() 
     { } 

     public class PostContext : DbContext 
     { 
      public DbSet<Post> Posts { get; set; } 
     } 
    } 
} 

Controller:

using static class_project.Models.Post; // I AM NOT SURE ABOUT THIS PART, BUT IT WAS ABLE TO BUILD WHEN ADDED IT. 

namespace class_project.Controllers 
{ 
    public class PostsController : Controller 
    { 
     private PostContext db = new PostContext(); 

     public ActionResult Create(Post obj) 
     { 
      if (!ModelState.IsValid) 
      { 
       return View(obj); 
      } 

      db.Posts.Add(obj); 

      return RedirectToAction("Index", "Posts"); 
     } 
    } 
} 

Und meine View ist ein automatisch generierter Standard Create mit zwei Eingabefeldern (ich nehme an, dass die ID aus der db generiert wird) basierend auf dem Modell.

@model class_project.Models.Post 

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

    <div class="form-horizontal"> 
     <h4>Post</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

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

Sie vermissen db.SaveChanges nach dem db.Posts.Add – hdrdiab

Antwort

4

Sie müssen db.SaveChanges anrufen

Verwandte Themen