简体   繁体   中英

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. 订单和产品之间的一对多关系

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 });
}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM