c# - Trying to create a Forum in MVC 5 / ASP.NET -


i'm trying create basic forum in asp.net mvc 5 , stuck @ "the insert statement conflicted foreign key constraint" error. post class , controller assignment , image illustrates how want work.

thread.cs

namespace blogproject.models {     public class thread     {         [key]         public int threadid { get; set; }         public datetime? postdate { get; set; }         public string threadtext { get; set; }         public string threadtitle { get; set; }         public virtual applicationuser userprofile { get; set; }         public string getusername { get; set; }          public virtual icollection<post> posts { get; set; }     }     public class post     {         public int postid { get; set; }         public string posttitle { get; set; }         public string posttext { get; set; }         public datetime? postdate { get; set; }         public virtual applicationuser userprofile { get; set; }          public virtual thread thread { get; set; }         //[foreignkey("thread")]         public int threadid { get; set; }      } } 

postscontroller.cs

// post: posts/create // protect overposting attacks, please enable specific properties want bind to,  // more details see http://go.microsoft.com/fwlink/?linkid=317598. [httppost] [validateantiforgerytoken] public actionresult create([bind(include = "postid,posttitle,posttext,postdate,threadid")] post post,thread thread) {     if (modelstate.isvalid)     {         db.posts.add(post);         db.savechanges();         return redirecttoaction("threads");     }      viewbag.threadid = new selectlist(db.threads, "threadid", "threadtext", post.threadid);     return view("details"); } 

this how want work:

forum goal if think information here not sufficient tell me , add more.

thanks in advance!

update edit:

sorry, forgot include views

details.cshtml, threads go

@model blogproject.models.thread  @{     viewbag.title = "details"; }  <h2>details</h2>  <div class="container">     <h3>current thread: @html.displayfor(model => model.threadtitle), posted by: @html.displayfor(modelitem => model.getusername)</h3>      <div class="panel panel-default">         <div class="panel-body">             <div class="col-lg-2">                 <div class="thumbnail">                     <img class="img-responsive user-photo" src="https://ssl.gstatic.com/accounts/ui/avatar_2x.png">                 </div>              </div>              <div class="well-lg">                 <div class="col-lg-10">                     @html.displayfor(model => model.threadtext)                 </div>             </div>         </div>     </div>      <p class="text-center">         @html.actionlink("reply thread","create","posts")         @html.actionlink("back list", "index")     </p> </div> 

posts/create.cshtml (partial view)

@model blogproject.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.posttext, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.posttext, new { htmlattributes = new { @class = "form-control" } })                 @html.validationmessagefor(model => model.posttext, "", 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 list", "index") </div> 

i post more views if needed. sorry taking long answer, i'm lying down on couch sleeping today.

first change postmodel because not assigning primary key , foreign key

public class post {     [key]     public int postid { get; set; }     .     public int threadid { get; set; }     [foreignkey("threadid")]     public virtual thread thread { get; set; } } 

in controller

[httppost] [validateantiforgerytoken] public actionresult create(thread thread) 

because in view page have used only

@model blogproject.models.thread 

the post object null , try 'thread'

db.posts.add(post); 

hope helps


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -