如何使用带有ASP.NET MVC的实体框架将外键保存为数据库中的null?

时间:2019-05-13 07:25:37

标签: c# asp.net asp.net-mvc

我的数据库中有三个表:Superdealersubdealercommission

  • Superdealer中的ID:SuperdealerId
  • 次级经销商中的编号:subdealerId
  • 委托编号:commissionId。它具有两个外键SuperdealerIdsubdealerId

有一个下拉列表,让我选择是将佣金分配给次级经销商还是超级经销商本身。如果我选择子经销商的值,则必须将其分配给subdealerid,而superdealerid应该为null。如果我选择超级经销商,则应该为其分配一个值,并且subdealerid应该为null。

我尝试了以下代码,但是该代码不属于我的问题。

 modelBuilder .Entity<Commission>()
                    .HasOptional<SuperDealer>(u => u.SuperDealer)
                    .WithOptionalPrincipal();
 modelBuilder .Entity<Commission>()
                    .HasOptional<SubDealer>(u => u.SubDealer)
                    .WithOptionalPrincipal();

public class Commission
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CommissionId { get; set; }


    //[Display(Name ="Selected Dealer Name")]
    //public string DealerName { get; set; }
    [Range(0, double.MaxValue, ErrorMessage = "Please enter valid 
    commission")]
    [Display(Name = "Commission Percentage")]
    public double CommissionPercentage { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Commission Start Date")]
    public DateTime CommissionStart { get; set; }
    [Display(Name = "First Commission")]
    [Range(0, int.MaxValue, ErrorMessage = "Please enter valid number")]
    public int firstCommissionPart { get; set; }
    [Display(Name = "Second Commission")]
    [Range(0, int.MaxValue, ErrorMessage = "Please enter valid number")]
    public Nullable<int> secondCommissionPart { get; set; }
    [Display(Name = "Third Commission")]
    [Range(0, int.MaxValue, ErrorMessage = "Please enter valid number")]
    public Nullable<int> thirdCommissionPart { get; set; }

    [ForeignKey("SubDealerId")]
    public SubDealer SubDealer { get; set; }
    [Display(Name = "Select Sub dealer")]
    public int? SubDealerId { get; set; }

    [ForeignKey("SuperDealerId")]
    public SuperDealer SuperDealer { get; set; }
    [Display(Name = "Select Super dealer")]
    public int? SuperDealerId { get; set; }

 }

这是向经销商创建佣金的代码

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CommissionId,CommissionPercentage,CommissionStart,firstCommissionPart,secondCommissionPart,thirdCommissionPart,SubDealerId,SuperDealerId")] Commission commission, FormCollection fc)
{
        //var superdealer = Request.Form["SuperDealerId"];
        //var subdealer = Request.Form["SubDealerId"];
        int superdealer = int.Parse(fc["SuperDealerId"]);
        int subdealer = int.Parse(fc["SubDealerId"]);


        if (ModelState.IsValid)
        {
            if (superdealer > 0 && subdealer > 0)
            {
                commission.SubDealerId = subdealer;
            }
            else if (superdealer > 0)
            {
                commission.SuperDealerId = superdealer;
            }
            db.Commissions.Add(commission);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        List<SelectListItem> plans = new List<SelectListItem>();

        plans.Add(new SelectListItem { Text = "QM Plan", Value = "1", Selected = true });

        plans.Add(new SelectListItem { Text = "Customized Plan", Value = "2" });
        ViewBag.Plans = plans;
        ViewBag.SubDealerId = new SelectList(db.SubDealers, "SubDealer_No", "Name", commission.SubDealerId);
        ViewBag.SuperDealerId = new SelectList(db.SuperDealers, "SuperDealer_No", "Name", commission.SuperDealerId);
        return View(commission);
}

我想要的是

  1. 一个超级经销商下可以有许多子经销商。有一个下拉列表,让我选择是将佣金分配给次级经销商还是超级经销商本身。如果我选择子经销商的值,必须将其分配给subdealerid,而superdealerid应该为null。如果我选择超级经销商,则应为其分配一个值,并且subdealerid应该为null。

单击时,出现以下错误:https://imgur.com/sWsrKnZ

0 个答案:

没有答案