gpt4 book ai didi

mysql - 我必须首先将第一个表的主键存储到 MVC Entity Framework 数据库中的第二个表。 MVC 中的外键

转载 作者:行者123 更新时间:2023-11-30 21:35:19 24 4
gpt4 key购买 nike

我已经尝试过 Tempdata,全局声明和 session 使用 MVC 和 Entity Framework 6 db 优先方法

public class CustomerModel
{
//i have to pass or store cusid to paymentModel

public int Cusid { get; set; }
[Required(ErrorMessage = "Enter username")]
public string Cusname { get; set; }
[Required(ErrorMessage = "Enter Phone")]
public string Cusphone { get; set; }
[Required(ErrorMessage = "Enter Address")]
public string Cusaddress { get; set; }
[Required(ErrorMessage = "select gender")]
public Nullable<int> Gid { get; set; }
//[Required(ErrorMessage = "Enter UID number")]
public string Cusaadhar { get; set; }


}


public class PaymentModel
{
public int Id { get; set; }
[Required(ErrorMessage ="select Brand")]
public Nullable<int> Brandid { get; set; }
[Required(ErrorMessage = "Enter Model")]
public string Model { get; set; }
[Required(ErrorMessage = "Enter Product")]
public string Product { get; set; }
[Required(ErrorMessage = "Enter IMEI number")]
public string Imei { get; set; }
[Required(ErrorMessage = "Enter Amount")]
public string Amount { get; set; }
[Required(ErrorMessage = "Enter Discount")]
public string Discount { get; set; }
[Required(ErrorMessage ="select date")]
public Nullable<System.DateTime> Paymentdate { get; set; }
public Nullable<int> Mid { get; set; }
public Nullable<int> Cusid { get; set; }
public Nullable<int> Empid { get; set; }
public Nullable<int> CusMid { get; set; }

}

这是我的 Controller ..

       public ActionResult CustomerSave(CustomerModel CusModel)
{
db_mobilestoreEntities2 ent = new db_mobilestoreEntities2();
tbl_Customer cus = new tbl_Customer();

cus.Cusname = CusModel.Cusname;
cus.Cusphone = CusModel.Cusphone;
cus.Cusaddress = CusModel.Cusaddress;
cus.Gid = CusModel.Gid;
cus.Cusaadhar = CusModel.Cusaadhar;

ent.tbl_Customer.Add(cus);
ent.SaveChanges();

        int latestid = cus.Cusid;
TempData["cusid"] = latestid;

return RedirectToAction("PaySave");
}

public ActionResult PaySave(PaymentModel Pmodel)
{
db_mobilestoreEntities2 ent = new db_mobilestoreEntities2();

tbl_Payment pay = new tbl_Payment();
pay.Brandid = Pmodel.Brandid;
pay.Model = Pmodel.Model;
pay.Product = Pmodel.Product;
pay.Imei = Pmodel.Imei;
pay.Amount = Pmodel.Amount;
pay.Discount = Pmodel.Discount;
pay.Paymentdate = DateTime.Now;
pay.Cusid=??
pay.CusMid = Pmodel.CusMid;
pay.Mid = Pmodel.Mid;

ent.tbl_Payment.Add(pay);
ent.SaveChanges();
int latestid = pay.Id;
return RedirectToAction("Details");
}

有两个表。

1st is tbl_customer

2nd is tbl_paysave

提交第一页重定向到第二页后,我必须将 customerid(主键)存储到 tbl_paysave(外键,即 Cusid),即 Paysave

最佳答案

您可以发送如下所示的参数值

获取插入的master或者header的id

int headerId = 0;
_db.ProductMasters.Add(objProdmst);
_db.SaveChanges();
headerId = objProdmst.HeaderId;

现在在你的重定向中尝试

return RedirectToAction("Index", "SavePayDetails", new { iHeaderId : headerId})

通过这种方式,您可以执行进一步的操作,即SaveUpdate

关于mysql - 我必须首先将第一个表的主键存储到 MVC Entity Framework 数据库中的第二个表。 MVC 中的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54179493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com