您好,我正在尝试做一些我不是真正的专家的工作,到目前为止,我的帮助很大。
我有一张桌子和一张这样的表格
当表格转到[httppost]方法时,该表由存储在会话中的数据表填充。我的问题是,当我单击“保存”按钮时,正在保存表单数据,而不是表格
我的视图如下
@using cgs.Models;
@model ConfirmOrderCustomModel
@using System.Data;
@using System.Net;
@{
ViewBag.Title = "EditProduct";
Layout = "~/Views/Shared/DashboardLayout.cshtml";
}
<div id="form-wrapper" class="page-wrapper">
<div id="overview-row" class="row">
<div class="col-lg-12">
<h1 class="page-header">Confirm Order</h1>
</div>
</div>
<div id="form-div" class="row edit-form">
@using (Html.BeginForm("ConfirmOrder", "Product", FormMethod.Post))
{
@Html.AntiForgeryToken()
@*@Html.ValidationSummary(false, "", new { @class = "text-danger" })*@
<div class="row">
<div class="col-lg-8">
<table id="calculate-table" class="rwd-table">
<tr>
<th>ID</th>
<th>Order Units</th>
<th>Total</th>
<th>Discount</th>
<th>Net</th>
</tr>
@{
if (Session["tblCart"] != null)
{
DataTable dt = (DataTable)Session["tblCart"];
foreach (DataRow row in dt.Rows)
{
<tr>
@foreach (DataColumn col in dt.Columns)
{
<td>@row[col.ColumnName]</td>
}
<td><input type="button" value="Remove" onclick="Remove(this)" /></td>
</tr>
}
}
}
</table>
</div>
</div>
<div class="row">
<div class="col-lg-8">
@Html.LabelFor(model => model.Orders.ShippingDate, new { htmlAttributes = new { @placeholder = "Shipping Date", @id = "lblShippingDate", @class = "label" } })
@Html.EditorFor(model => model.Orders.ShippingDate, new { htmlAttributes = new { @type = "date", @min = 0, @value = "", @placeholder = "Shipping Date", @name = "shippingDate", @id = "shippingDate", @required = "" } })
@Html.ValidationMessageFor(model => model.Orders.ShippingDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-lg-8">
@Html.LabelFor(model => model.Orders.OrderTotal, new { htmlAttributes = new { @placeholder = "Total", @id = "lblTotal", @class = "label" } })
@Html.EditorFor(model => model.Orders.OrderTotal, new { htmlAttributes = new { @type = "text", @min = 0, @value = "", @placeholder = "Total", @name = "total_price", @id = "total_price", @required = "" } })
@Html.ValidationMessageFor(model => model.Orders.OrderTotal, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-lg-8">
@Html.LabelFor(model => model.Orders.CustomerName, new { htmlAttributes = new { @placeholder = "Customer", @id = "lblCustomer", @class = "label" } })
@Html.EditorFor(model => model.Orders.CustomerName, new { htmlAttributes = new { @type = "text", @min = 0, @value = "", @placeholder = "Customer", @name = "cust_name", @id = "cust_name", @required = "" } })
@Html.ValidationMessageFor(model => model.Orders.CustomerName, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-lg-8">
@Html.LabelFor(model => model.Orders.CustomerPhone, new { htmlAttributes = new { @placeholder = "CustomerPhone", @id = "lblPhone", @class = "label" } })
@Html.EditorFor(model => model.Orders.CustomerPhone, new { htmlAttributes = new { @type = "text", @min = 0, @value = "", @placeholder = "Customer Phone", @name = "cust_phone", @id = "cust_phone", @required = "" } })
@Html.ValidationMessageFor(model => model.Orders.CustomerPhone, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-lg-8">
@Html.LabelFor(model => model.Orders.CustomerEmail, new { htmlAttributes = new { @placeholder = "Email", @id = "lblEmail", @class = "label" } })
@Html.EditorFor(model => model.Orders.CustomerEmail, new { htmlAttributes = new { @type = "text", @min = 0, @value = "", @placeholder = "Email", @name = "cust_email", @id = "cust_email", @required = "" } })
@Html.ValidationMessageFor(model => model.Orders.CustomerEmail, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-lg-8">
@Html.LabelFor(model => model.Orders.DeliveryAddress, new { htmlAttributes = new { @placeholder = "Delivery Address", @id = "lblAddress", @class = "label" } })
@Html.EditorFor(model => model.Orders.DeliveryAddress, new { htmlAttributes = new { @type = "text", @min = 0, @value = "", @placeholder = "Address", @name = "cust_address", @id = "cust_Address", @required = "" } })
@Html.ValidationMessageFor(model => model.Orders.DeliveryAddress, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-offset-4 col-md-4">
<button id="btnSave" name="action" value="confirm">Confirm Order</button>
</div>
</div>
}
</div>
</div>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
jQuery(document).ready(function() {
setTimeout(function() {
var sum1 = 0;
$("#calculate-table tr").not(':first').each(function () {
sum1 += getnum($(this).find("td:eq(4)").text());
function getnum(t) {
if (isNumeric(t)) {
return parseInt(t, 10);
}
return 0;
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
}
});
$("#total_price").val(sum1);
}, 1000);
});
function Remove(button) {
//Determine the reference of the Row using the Button.
var row = $(button).closest("TR");
var name = $("TD", row).eq(0).html();
if (confirm("Do you want to delete: " + name)) {
//Get the reference of the Table.
var table = $("#calculate-table")[0];
//Delete the Table row using it's Index.
table.deleteRow(row[0].rowIndex);
}
};
$("body").on("click", "#btnSave", function () {
//Loop through the Table rows and build a JSON array.
var orderProducts = new Array();
$("#calculate-table tr").each(function () {
var row = $(this);
var products = {};
products.ProductID = row.find("td").eq(0).html();
products.OrderUnits = row.find("td").eq(1).html();
products.TotalSum = row.find("td").eq(2).html();
products.DiscountVal = row.find("td").eq(3).html();
products.NetSum = row.find("td").eq(4).html();
orderProducts.push(products);
});
//Send the JSON array to Controller using AJAX.
$.ajax({
type: "POST",
url: "/Product/ConfirmOrder",
data: JSON.stringify(orderProducts),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (json) {
if (json.isRedirect) {
window.location.href = json.redirectUrl;
}
}
});
});
</script>
和我的操作结果
[HttpPost]
public JsonResult ConfirmOrder(ConfirmOrderCustomModel model)
{
using(dbcontext = new CDBContext())
{
dbcontext.Orders.Add(new Model.Orders
{
ShippingDate = model.Orders.ShippingDate,
OrderTotal = model.Orders.OrderTotal,
CustomerName = model.Orders.CustomerName,
CustomerPhone = model.Orders.CustomerPhone,
CustomerEmail = model.Orders.CustomerEmail,
DeliveryAddress = model.Orders.DeliveryAddress,
OrderStatus = true
});
dbcontext.SaveChanges();
int id = dbcontext.Orders.Max(odr => odr.ID);
DataTable dt = (DataTable)Session["tblCart"];
List<DataRow> list = new List<DataRow>(dt.Select());
foreach (var item in model.SalesModel)
{
dbcontext.OrderProducts.Add(new OrderProducts
{
OrderUnits = item.OrderUnits,
TotalSum = item.TotalSum,
DiscountVal = item.DiscountVal,
NetSum = item.NetSum,
ProductID = item.ProductID,
OrderID = id
});
dbcontext.SaveChanges();
}
return Json(new
{
redirectUrl = Url.Action("Dashboard", "Admin"),
isRedirect = true
});
}
}
感谢您的帮助