Muhammad Arslan Jamshaid
Muhammad Arslan Jamshaid

Reputation: 1197

Inserting Data in Object with Hard Coded Form in ASP.Net MVC

I have two tables Product & Orders, What I did was, I created View and and Hard Coded HTML form that passes the Ordered Products into Order Object. While saving the Orders I am getting error INSERT statement conflicted with the FOREIGN KEY constraint I have added the breakpoints as well, all values are being properly filled in Orders Object, but Id & Products property is null.

I have created a One to Many Relationship as seen in image below. Relationship one to many between orders and products

This is my view

@model Myapp.Areas.Admin.ViewModel.ProductDisplayViewModel
@ViewBag.Message    
    @using (Html.BeginForm("OrderProduct", "Order"))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)

        <div class="form-group">
            <label>Your Name</label>           
            <input id="Name" name="Name" class="form-control" type="text"/>
        </div>

            <div class="form-group">
        <label>Your Email</label>
        @Html.TextBox("Email", null, new { @class = "form-control" })
    </div>
    <div class="form-group">
        <label>Your Contact Number</label>
        @Html.TextBox("ContactNumber", null, new { @class = "form-control" })
    </div>
    <div class="form-group">
        <label>Your Address</label>
        @Html.TextBox("Address", null, new { @class = "form-control" })
    </div>
    <div class="form-group">
        <label>Quantity</label>
        @Html.TextBox("Quantity", null, new { @class = "form-control",type="Number" })
    </div>
    <div class="form-group">
        <label>Any Comments</label>
        @Html.TextBox("Comments", null, new { @class = "form-control" })
    </div>
    <div class="form-group">
    @Html.Hidden("ProductId", Model.ProductId)
    </div>
    <div class="form-group">        
        <input id="Submit1" type="submit" value="Order Now" class="read-more"/>        
        <a href="@Url.Action("Index")" class="read-more">Back to Products</a>

    </div>
        }

This is my Action Method in Orders Controller

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult OrderProduct(Orders order)
{
    if (ModelState.IsValid)
    {
        db.Orders.Add(order);
        db.SaveChanges();
        // if Order Saved in DB show success Message and Redirect on the same product page
        ViewBag.Message = "Value Entered in DB";
        return RedirectToAction("Details", "Product", new { id = order.ProductId });
    }
    // if something went wrong Redirect on the same product page
    return RedirectToAction("Details", "Product", new { id = order.ProductId });
}

Upvotes: 0

Views: 2019

Answers (1)

Eric Kelly
Eric Kelly

Reputation: 452

ProductId should be the Id from the products table, going by your definition but you're passing Model.ProductId which would cause your issue.

Populate the ProductId field of the model you're returning with the Product.Id from the product your adding the order for.

newOrderObject.ProductId = Product.Id
return View(newOrderObject);

Upvotes: 1

Related Questions