[英]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. 我有两个表Product
& Orders
,我所做的是,创建了View
和Hard Coded HTML表单,该表单将Ordered Products
传递到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. 保存Orders
我收到INSERT statement conflicted with the FOREIGN KEY constraint
错误INSERT statement conflicted with the FOREIGN KEY constraint
我也添加了断点,所有值均已正确填充到Orders对象中,但Id
和Products
属性为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. ProductId应该是产品表中的ID,按照您的定义,但是您传递的是Model.ProductId,这会导致问题。
Populate the ProductId field of the model you're returning with the Product.Id from the product your adding the order for. 使用您要为其添加订单的产品中的Product.Id填充要返回的模型的ProductId字段。
newOrderObject.ProductId = Product.Id
return View(newOrderObject);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.