C# 外键列显示提交创建或编辑表单成功后的空白单元格

C# 外键列显示提交创建或编辑表单成功后的空白单元格,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我的问题:当我成功提交编辑或创建采购操作时,供应商(采购表中的外键)的值为空 我的问题:如何获取提交到表中的外键值,而不是获取null值 这是我的模型: public class Purchase { [Required(ErrorMessage = "Invoice No. must be uniqe!")] [Display(Name = "Invoice No.")] public string Id { get; set; } [Display(Nam

我的问题:当我成功提交编辑或创建采购操作时,供应商(采购表中的外键)的值为空

我的问题:如何获取提交到表中的外键值,而不是获取null值

这是我的模型:

 public class Purchase
{

    [Required(ErrorMessage = "Invoice No. must be uniqe!")]
    [Display(Name = "Invoice No.")]
    public string Id { get; set; }
    [Display(Name = "yyyy-MM-dd")]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime date { get; set; }
    //[Display(Name = "Supplier")]
    //public int supplier_id { get; set; }
    [Display(Name = "Qty")]
    public decimal amount { get; set; }
    [Display(Name = "Discount")]
    public decimal? discount { get; set; }
    [Display(Name = "Total")]
    [Required]
    public decimal grand_total { get; set; }
    [Display(Name = "Paid?")]
    public bool is_paid { get; set; }
    [Display(Name = "Last update")]
    public DateTime? last_updated { get; set; }
    [Display(Name = "Description")]
    public string description { get; set; }


    public virtual Supplier Supplier { get; set; }
    public virtual ICollection<PurchaseProduct> PurchaseProducts { get; set; }
}



public class Supplier
{
    public Supplier()
    {
        this.address = "N/A";
    }
    public int ID { get; set; }

    [Required]
    [StringLength(50, ErrorMessage = "Only 50 characters allowed!")]
    public string name { get; set; }

    public string address { get; set; }
    public string contact { get; set; }
    public string description { get; set; }

    public virtual ICollection<Purchase> Purchases { get; set; }
}
这是我创建视图中的下拉列表:

@model PointOfSales.Models.Purchase
        <div class="form-group">
            @Html.LabelFor(model => model.Supplier, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Supplier, (SelectList)ViewBag.MyHeaders2, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Supplier, "", new { @class = "text-danger" })
            </div>
        </div>
@model PointOfSales.Models.Purchase
@LabelFor(model=>model.Supplier,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.DropDownListFor(model=>model.Supplier,(SelectList)ViewBag.MyHeaders2,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Supplier,“,new{@class=“text danger”})

不能将
绑定到复杂对象(将
绑定到回发简单值的对象)。您的模型需要一个导航属性(通常类型为
int
,它是
供应商
表的FK),您可以绑定到该属性。@StephenMuecke抱歉,但它确实从供应商表中选择了所有供应商,我在我知道的下拉列表中获取了这些供应商,但问题是,您正试图绑定到您的
供应商
,这是一个复杂的对象,而这不能像我在第一次评论中所说的那样完成。您需要绑定到
int
属性。我建议您也阅读我建议您阅读并停止在视图中使用数据模型通过使用包含
int SelectedSupplier
IEnumerable SupplierOptions
属性的视图模型来执行此属性您还希望阅读,因为POST方法中的代码将引发该异常
@model PointOfSales.Models.Purchase
        <div class="form-group">
            @Html.LabelFor(model => model.Supplier, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Supplier, (SelectList)ViewBag.MyHeaders2, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Supplier, "", new { @class = "text-danger" })
            </div>
        </div>