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

I'm trying to create a basic forum in ASP.NET MVC 5 and I am really stuck at a "The INSERT statement conflicted with the FOREIGN KEY constraint" error.
I will post my class and controller for this assignment and an image that illustrates how I want this to 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
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// 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 is how I want it to work:
Forum goal
If you think the information here is not sufficient then tell me and I will add more.
Thanks in advance!
UPDATE EDIT:
Sorry, forgot to include my Views
Details.cshtml, this is where my 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 to this thread","Create","Posts")
#Html.ActionLink("Back to 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 to List", "Index")
</div>
I will post more Views if needed. Sorry for taking so long to answer, I'm mostly lying down on the couch sleeping today.

First change the Postmodel because your not assigning Primary Key and 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 the controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Thread thread)
Because in the view page you have used only
#model BlogProject.Models.Thread
The post object will be null and try 'thread'
db.Posts.Add(post);
hope this helps

Related

Validation in ViewModel not working

I have ViewModel which contains some proprty of class. Code below.
public class ViewModel
{
public Doctor VmDoctor { get; set; }
public Patient VmPatient { get; set; }
public List<Visit> VmVisit { get; set; }
public List<Hours> hours { get; set; }
public List<Hours> hours2 { get; set; }
public Schedule schedule { get; set; }
public bool BlockBtn { get; set; }
public Test test { get; set; }
}
In this case important property is Patient VmPatient. This is a model which has been generated by Database Model First. He has validation.Code below.
public partial class Patient
{
public Patient()
{
this.Visits = new HashSet<Visit>();
}
public int PatientID { get; set; }
[Required(ErrorMessage = "Podaj imię.")]
public string name { get; set; }
[Required(ErrorMessage = "Podaj nazwisko.")]
public string surname { get; set; }
[Required(ErrorMessage = "Podaj pesel.")]
[RegularExpression(#"^\(?([0-9]{11})$", ErrorMessage = "Nieprawidłowy numer pesel.")]
public string pesel { get; set; }
[Required(ErrorMessage = "Podaj miasto.")]
public string city { get; set; }
[Required(ErrorMessage = "Podaj kod pocztowy.")]
public string zipCode { get; set; }
[Required(ErrorMessage = "Podaj e-mail.")]
[EmailAddress(ErrorMessage = "Nieprawidłowy adres e-mail")]
public string email { get; set; }
[Required(ErrorMessage = "Podaj telefon komórkowy.")]
[RegularExpression(#"^\(?([0-9]{9})$", ErrorMessage = "Nieprawidłowy numer telefonu.")]
public string phone { get; set; }
public virtual ICollection<Visit> Visits { get; set; }
}
and i have Main Index where return my ViewModel because, display two Models in the same View. Code below
public ActionResult Index(int id)
{
ViewModel _viewModle = new ViewModel();
schedule = new Schedule();
if(Request.HttpMethod == "Post")
{
return View(_viewModle);
}
else
{
idDr = id;
_viewModle.schedule = schedule;
_viewModle.BlockBtn = _repository.BlockBtn(schedule);
_viewModle.VmDoctor = db.Doctors.Find(idDr);
_viewModle.hours = _repository.GetHours();
foreach (var item in _viewModle.hours)
{
_viewModle.hours2 = _repository.GetButtonsActiv(item.hourBtn, item.count, idDr, schedule);
}
}
if (_viewModle == null)
{
return HttpNotFound();
}
return View(_viewModle);
}
inside View Index i display my objects and rendered partial _FormPatient.Code below.
#model Dentist.Models.ViewModel
<div class="container-select-doctor">
<div class="row">
<div class="text-left">
<div class="row">
<div class="content">
<div class="profileImage">
<div class="imageContener"><img style="margin:1px;" src="#Url.Content("~/Images/" + System.IO.Path.GetFileName(#Model.VmDoctor.image))" /></div>
</div>
<div class="profileInfo">
<div class="profileInfoName">#Model.VmDoctor.name #Model.VmDoctor.surname</div>
<div class="profileInfoSpeciality">#Model.VmDoctor.specialty</div>
</div>
</div>
</div>
</div>
#ViewBag.firstDay<br />
#ViewBag.lastDay<br />
<div class="text-middle">
<div class="content">
<div id="partialZone">
#Html.Partial("_TableSchedule")
</div>
</div>
</div>
<div class="text-right">
<div class="content">
#Html.Partial("_FormPatient")
</div>
</div>
</div>
</div>
and last step is form which has been rendered inside Main Index by #Html.partial.code below
#model Dentist.Models.ViewModel
#using (Html.BeginForm("Create","Patient"))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<font color="red">#ViewBag.Pesel</font>
<div class="form-horizontal">
<div class="form-group">
#Html.LabelFor(model => model.VmPatient.email, htmlAttributes: new { #class = "control-label col-md-2" }, labelText: "E-mail:")
<div class="col-md-10">
#Html.TextBoxFor(model => model.VmPatient.email, new { htmlAttributes = new { #class = "form-control" } })
#*<input class="form-control" id="email" name="email" type="text" value="">*#
#Html.ValidationMessageFor(model => model.VmPatient.email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.VmPatient.phone, htmlAttributes: new { #class = "control-label col-md-2" }, labelText: "Telefon kom.:")
<div class="col-md-10">
#Html.TextBoxFor(model => model.VmPatient.phone, new { maxlength = 9 })
#*<input class="form-control" maxlength="9" id="phone" name="phone" type="text" value="" />*#
#Html.ValidationMessageFor(model => model.VmPatient.phone, "", 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>
}
Please pay attention that this form redirect to another Controller where data will be validate and save to database. Method where data from FORM will be validate and save. code below
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Patient pat)
{
ViewModel vm = new ViewModel();
DentistEntities db = new DentistEntities();
if (ModelState.IsValid)
{
db.Patients.Add(pat);
db.SaveChanges();
}
return RedirectToAction("Index", "Visit", new { id = VisitController.idDr });
}
Conclusion How can i get validation for this form! I have observed that,every time modelstate.isvalid return false.. I dont have any ideas so I would like to ask you for help.
Best regards.
I would suggest that you do this:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Patient pat)
{
ViewModel vm = new ViewModel();
DentistEntities db = new DentistEntities();
if (ModelState.IsValid)
{
db.Patients.Add(pat);
db.SaveChanges();
}
vm.VmPatient = pat;
return View(vm);
}
Render the view again, but this time the validation error messages should appear on the page (via the ValidationMessageFor() calls in the view). That, at least you can see why the validation has failed.
Alternatively, you could interrogate the modelstate e.g.
foreach (ModelState modelState in ViewData.ModelState.Values) {
foreach (ModelError error in modelState.Errors) {
string error = error.ErrorMessage;
}
}

Create Item and add images for Item in one step

I am trying to learn ASP.net by making an e-commerce site. I am trying to set up the ability to create Items and assign Images to the item being created via File Upload.
I managed to get the multiple file upload working, but only to the content/Images folder. I cant figure out out to marry this to the creation of Items so you can assign multiple images to an Item all in the creation of the item.
It would be a fair to say I dont know where to go from here and would appreciate any help.
Item Model Class: Table in the database to store each item. Is referenced from the Images Table with a 1 to many relationship.
public class Item
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ItemId { get; set; }
public int CategoryId { get; set; }
public int DesignerId { get; set; }
public int ImageId { get; set; }
[Required]
[MaxLength(250)]
public string ItemName { get; set; }
[Required]
[Range(0,9999)]
public decimal ItemPrice { get; set; }
[MaxLength(1000)]
public string ItemDescription { get; set; }
[Range(4,22)]
public int ItemSize { get; set; }
[Column("CategoryId")]
public virtual List<Category> Category { get; set; }
public virtual List<OrderDetail> OrderDetails { get; set; }
public virtual List<Image> Images { get; set; }
}
Image Model Class: Stores the URL for each Image in the content Directory of the site. Can have many images for each Item.
public class Image
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ImageId { get; set; }
[Required]
public string ImageURL { get; set; }
[Required]
public string ItemId { get; set; }
//Files Being Uploaded by the User
public IEnumerable<HttpPostedFileBase> Files { get; set; }
[Column("ItemId")]
public virtual List<Item> Item { get; set; }
}
Store Manager Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Item item,HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
//The below successfully saves the file to the content folder when separated into the Index Action.
foreach (var f in item.Files)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(f.FileName);
var path = Path.Combine(Server.MapPath("~/Content/ItemImages/"+item), fileName);
file.SaveAs(path);
}
}
// The below also works when I dont have the Above in the Action.
db.Items.Add(item);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(item);
}
Create Item View
#model Project.Models.Item
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Item</legend>
<div class="editor-label">
#Html.LabelFor(model => model.ItemName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ItemName)
#Html.ValidationMessageFor(model => model.ItemName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ItemPrice)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ItemPrice)
#Html.ValidationMessageFor(model => model.ItemPrice)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ItemDescription)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ItemDescription)
#Html.ValidationMessageFor(model => model.ItemDescription)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ItemColour)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ItemColour)
#Html.ValidationMessageFor(model => model.ItemColour)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ItemSize)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ItemSize)
#Html.ValidationMessageFor(model => model.ItemSize)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
#using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
<table>
<tr>
<td>Files</td>
<td><input type="file" name="Files" id="Files" multiple/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Upload" /></td>
</tr>
</table>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
So it looks like you're pretty close.
You just have to add the images to the item before adding it to the database.
Since you're using EF, it should be something similar to this
//in your action
//add the images to the item
item.Images.Add(new Image { ImageUrl = ... });
//you should be able to just insert the whole entity graph here
db.Items.Add(item);
db.SaveChanges();
Something like that I think is what you're looking for.
Also in your model constructor normally I think you want to initalize those lists so you don't get null reference execeptions when doing something like the above
public class Item
{
public Item()
{
this.Images = new List<Image>();
}
//...
}

Cant seem the get my dropdown menu to work, spent hours looking through this and a tutorial, but I cant seem to get it right

I'm trying to get a dropdown menu along side my other text inputs, I've been tracking a music store tutorial for this specific feature, but I cant seem to get mine to work. I've been revising my code and comparing for hours but I cant see my errors, If anyone can help it would be greatly appreciated.
Here is my controller code:
// GET: /Default1/Create
public ActionResult Create()
{
ViewBag.CardTypeId = new SelectList(db.CardTypee, "CardTypeId", "Type");
return View();
}
//
// POST: /Default1/Create
[HttpPost]
public ActionResult Create(Card card)
{
if (ModelState.IsValid)
{
db.Cards.Add(card);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CardTypeId = new SelectList(db.CardTypee, "CardTypeId", "Type", card.CardTypeId);
return View(card);
}
My models that I have:
Card.cs
namespace ActualPayment.Models
{
public class Card
{
public int Id { get; set; }
public CardType CardType { get; set; }
public int CardTypeId { get; set; }
public string Name { get; set; }
public string CardNumber { get; set; }
public int SortCode { get; set; }
public int SecurityCode { get; set; }
public int ExpirationDate { get; set; }
}
}
CardType.cs
namespace ActualPayment.Models
{
public partial class CardType
{
[Key] public int CardTypeId { get; set; }
public string Type { get; set; }
}
}
CardTypes.cs
namespace ActualPayment.Models
{
public class CardTypes : DropCreateDatabaseIfModelChanges<CardPayment>
{
protected override void Seed(CardPayment context)
{
var cardType = new List<CardType>
{
new CardType { Type = "Visa/Delta/Electron" },
new CardType { Type = "Master Card/Euro Card" },
new CardType { Type = "American Express" },
new CardType { Type = "Solo/Maestro" },
new CardType { Type = "Maestro" },
};
}
}
}
CardPayments.cs
namespace ActualPayment.Models
{
public class CardPayment : DbContext
{
public DbSet<CardType> CardTypee { get; set; }
public DbSet<Card> Cards { get; set; }
}
}
And my view:
#model ActualPayment.Models.Card
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Card</legend>
<div class="editor-label">
#Html.LabelFor(model => model.CardTypeId, "CardType")
</div>
<div class="editor-field">
#Html.DropDownList("CardTypeId", String.Empty)
#Html.ValidationMessageFor(model => model.CardTypeId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.CardNumber)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.CardNumber)
#Html.ValidationMessageFor(model => model.CardNumber)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.SortCode)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.SortCode)
#Html.ValidationMessageFor(model => model.SortCode)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.SecurityCode)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.SecurityCode)
#Html.ValidationMessageFor(model => model.SecurityCode)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ExpirationDate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ExpirationDate)
#Html.ValidationMessageFor(model => model.ExpirationDate)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
Seems like there's nothing wrong with the code.
Chrome capture for posted values
value at action
Did you mean the CardTypeId is not getting populated??

MVC3/C#/Razor/EF handling data from multiple partial 'create views'

For registering a new customer I have several partials on my view which refer to corresponding tables in my database.
Primary keys are identity fields, that's why no IDs on the view. To insert a new customer I need to insert 3 occurrences of address(visiting, postal and contact person's), then insert a row for contract, contact_person(with using addressId from one of already inserted addresses) and finally proceed with inserting a new customer which will contain foreign keys referencing to just inserted visiting and postal addresses, contract and contact person.
Could you recommend the best/easiest way to pass the data from these partial views as objects to CustomerController and handle the objects there please?
Links to examples of handling similar situations will be also highly appreciated.
Image of my page:
http://i1345.photobucket.com/albums/p673/swell_daze/customer_registration_zps563341d0.png
Image of tables:
http://i1345.photobucket.com/albums/p673/swell_daze/tables_zpsc60b644a.png
View code:
#model CIM.Models.customer
<h2>Register new customer</h2>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<fieldset>
<legend>New customer registration</legend>
<fieldset class="span3">
<legend>Basic information</legend>
<div class="editor-label">
#Html.LabelFor(model => model.name, "Name")
</div>
<div class="editor-field">
#Html.EditorFor(model => model.name)
#Html.ValidationMessageFor(model => model.name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.groupId, "Customer group")
</div>
<div class="editor-field">
#Html.DropDownList("groupId", String.Empty)
#Html.ValidationMessageFor(model => model.groupId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.status, "Status")
</div>
<div class="editor-field">
#Html.DropDownList("status")
#Html.ValidationMessageFor(model => model.status)
</div>
</fieldset>
<fieldset class="span3">
<legend>Visiting address</legend>
<div>
#{
Html.RenderPartial("AddressPartial");
}
</div>
</fieldset>
<fieldset style="width:270px">
<legend>Postal address</legend>
<div>
#{
Html.RenderPartial("AddressPartial");
}
</div>
</fieldset>
<fieldset class="span3">
<legend>Contract</legend>
<div>
#{
Html.RenderPartial("ContractPartial");
}
</div>
</fieldset>
<fieldset class="span3" style="width:540px">
<legend>Contact person</legend>
<div>
#{
Html.RenderPartial("ContactPersonPartial");
}
</div>
<p>
<input type="submit" class="btn btn-success" value="Submit" style="margin-right:1em"/>
#Html.ActionLink("Cancel", "Index", "Customer", new {#class="btn btn-danger", #type="button"})
</p>
</fieldset>
</fieldset>
}
you can wrap your view data in a viewmodel, then when you submit your data it will be mapped to your viewmodel, something like this:
Create a viewmodel:
public class MyViewModel
{
public string name { get; set; }
public string someprop1 { get; set; }
public string someprop2 { get; set; }
public MyAddress visitingaddress { get; set; }
public MyAddress postaladdress { get; set; }
public MyContactAddress contactaddress { get; set; }
}
public class MyAddress
{
public string town { get; set; }
public string street { get; set; }
public string housenumber { get; set; }
public string postalcode { get; set; }
}
public class MyContactAddress : MyAddress
{
public string firstname { get; set; }
public string lastname { get; set; }
public string email { get; set; }
public string phonenumber { get; set; }
}
Create your view just like you have done but by using your viewmodel instead of the model you are using now (CIM.Models.customer), and then in your partials, for example in the postal address partial view do something like this:
.......
#Html.EditorFor(x => x.postaladdress.town)
......
Create your controller actions by using your viewmodel:
public ActionResult Index()
{
MyViewModel vm = new MyViewModel();
//doing something here....
return View(vm);
}
[HttpPost]
public ActionResult Index(MyViewModel vm)
{
//save your data, here your viewmodel will be correctly filled
return View(vm);
}
hope this helps

I hit an error when I try to add a new comment to my database

I run into an error that says:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Comments_dbo.Posts_PostID". The conflict occurred in database "MVCProjectApp.Models.DBPostsContext", table "dbo.Posts", column 'PostID'.
The statement has been terminated.
I am lost and not sure what to do
Here is the create comment
#model MVCProjectApp.Models.Comment
#{
ViewBag.Title = "Create Comment";
}
<h2>Create</h2>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Comments</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Username)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Username)
#Html.ValidationMessageFor(model => model.Username)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Message)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Message)
#Html.ValidationMessageFor(model => model.Message)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Timestamp)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Timestamp)
#Html.ValidationMessageFor(model => model.Timestamp)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "~/FullPost/Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
This is the Actionlink where I am passing the postid to the comment create
//
// GET: /Comment/Create
public ActionResult Create()
{
ViewBag.PostID = new SelectList(db.Posts, "PostID", "Title");
return View();
}
//
// POST: /Comment/Create
[HttpPost]
public ActionResult Create(Comment comment)
{
if (ModelState.IsValid)
{
db.Comments.Add(comment);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.PostID = new SelectList(db.Posts, "PostID", "Title", comment.PostID);
return View(comment);
}
here is the comment model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCProjectApp.Models
{
public class Post
{
public int PostID { get; set; }
public string Title { get; set; }
public string Message { get; set; }
public DateTime Timestamp { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
public class Comment
{
public int CommentID { get; set; }
public int PostID { get; set; }
public string Username { get; set; }
public string Message { get; set; }
public DateTime Timestamp { get; set; }
public virtual Post Post { get; set; }
}
}
I am lost and not sure what to do
You should take a look at the error you're getting. It says you're trying to insert a Comment. The foreign key constraint check that's ran by the DBMS before an insert tries to make sure the Comment has a PostID that it knows. You don't set it, so it'll default to 0, which does not exist in the Posts table, hence the error.
You should set the Comment's PostID before calling SaveChanges(), which can be done by adding a #Html.HiddenFor(m => m.PostID) field in the view. You then set the PostID of the model in the controller.
you need to associate the comment to the post. the simplest solution is to add a hidden field for the post id to the view.

Categories