【发布时间】:2020-11-06 13:28:47
【问题描述】:
我是 ASP.NET MVC 的新手。我面临一个问题,无法找到解决方案。实际上我正在创建一个库存管理项目。在这个项目中,我有 2 个 ViewModel。一个是 PurchaseOrderViewModel,另一个是 TableViewModel。现在我在弹出模式中使用两者并将详细信息保存在视图模型中,但我无法做到这一点。在图像中,您可以看到下面显示了一个表记录,我需要将其插入到视图模型中。请帮助我。我真的很感谢你。
[HttpPost]
public async Task<ActionResult> CreateOrUpdatePurchase(PurchaseOrderViewModel purchase)
{
PurchaseOrder purchase2 = new PurchaseOrder();
int suplierId = purchase.Supplier_ID;
purchase.Supplier_ID = suplierId;
purchase.SupplierName = context.Suppliers.Where(x => x.Supplier_ID == suplierId).Select(x => x.Supplier_Name).FirstOrDefault();
string loginId = Convert.ToString(Session["LoginId"]);
string UserRoleName = Convert.ToString(Session["UserTypeName"]);
try
{
if (!string.IsNullOrEmpty(loginId))
{
if (UserRoleName == RoleTypeConstant.CustomerUserType)
{
PurchaseOrder purchase1 = new PurchaseOrder();
purchase1.LoginID = loginId;
purchase1.PurchaseID = purchase.PurchaseID;
purchase1.SupplierID = purchase.Supplier_ID;
purchase1.SupplierName = purchase.SupplierName;
purchase1.Currency = purchase.Currency;
purchase1.Date_Of_Purchase = purchase.Date_Of_Purchase;
purchase1.Due_Date = purchase.Due_Date;
purchase1.CreatedBy = loginId;
purchase1.CreatedOn = DateTime.Now;
purchase1.UpdatedBy = loginId;
purchase1.UpdatedOn = DateTime.Now;
purchase1.Status = StatusConstant.Active;
bool regCustBen = await _websiteRepo.AddPurchase(purchase1);
if (regCustBen)
{
return Json(new { success = true, message = "Saved Successfully" }, JsonRequestBehavior.AllowGet);
}
return RedirectToAction("GetPurchasePartial");
}
else
{
return RedirectToAction("Purchase_Order", "Home");
}
}
else
{
return RedirectToAction("Login", "Account");
}
}
catch (Exception)
{
throw;
}
}
public class TableViewModel
{
public int Pid { get; set; }
//public string ProductName { get; set; }
public string Qty { get; set; }
public string Price { get; set; }
public string Amount { get; set; }
}
public class PurchaseOrderViewModel
{
public int PurchaseID { get; set; }
public string LoginID { get; set; }
public int SupplierID { get; set; }
public int Supplier_ID { get; set; }
public string SupplierName { get; set; }
public string Supplier_Name { get; set; }
public string Currency { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public string Date_Of_Purchase { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public string Due_Date { get; set; }
public string CreatedBy { get; set; }
public DateTime? CreatedOn { get; set; }
public string UpdatedBy { get; set; }
public DateTime? UpdatedOn { get; set; }
public int? Status { get; set; }
public int OrderID { get; set; }
public string ProductName { get; set; }
//public List<Order> s { get; set; }
public int Stock_ID { get; set; }
public string Stock_Name { get; set; }
public string Quantity { get; set; }
public decimal Price { get; set; }
public decimal Amount { get; set; }
public List<TableViewModel> TableListDto { get; set; }
}
我的脚本是
function CreateOrUpdate() {
debugger;
var modal = $("#purchaseModal");
var form = $('form[name="purchaseForm"]');
let tableData = $('#detailsTable > tbody > tr');
debugger;
console.log(tableData);
var tableDto = [];
$.each(tableData, function (rindex, row) {
console.log(row);
let tmp = {};
tmp.pid = $(row).find('td:nth-child(1)').attr('pid');
//tmp.productName = $(row).find('td:nth-child(1)').text();
tmp.qty = $(row).find('td:nth-child(2)').text();
tmp.price = $(row).find('td:nth-child(3)').text();
tmp.amount = $(row).find('td:nth-child(4)').text();
tableDto.push(tmp);
tmp = {};
});
form.validate();
if (!form.valid()) {
return;
}
else {
var data = form.serialize();
//data.TableListDto = tableDto.serialize();
$.post("/Home/CreateOrUpdatePurchase", data, function (res) {
if (res) {
modal.modal('hide');
dataTable.ajax.reload();
$.notify("Saved Successfully", {
className: "success",
globalPosition: 'top - center'
});
}
})
}
}
让我向您展示我的输出屏幕。 Click Here
【问题讨论】:
-
不要使用
nth-child(1)类型的东西,给td一个类以供使用 - 例如,您可以移动列而不必重做逻辑。 -
在您的方法中使用
purchase之前验证它 -
purchase.Supplier_ID = suplierId;?为什么?混淆Supplier_ID和SupplierID对我来说毫无意义 -
嗨,马克,如果我删除了第 n 个孩子(1),那么我怎样才能得到表中的值。你能为我提供正确的代码吗?请查看我刚刚在帖子中上传的输出屏幕。
-
标记,用于来自供应商表的下拉值,以保存视图模型中的值。
标签: javascript c# jquery asp.net-mvc datatables