当我保存我的下拉列表的值时,在数据库中将其设置为null

时间:2019-03-28 03:20:05

标签: asp.net-mvc-5

我在数据库中有一个类别列表,当我使用categoreyId将新组织保存到数据库时,categoreyId将保存到新ID,而不是在类别表中已经找到的类别之一。例如,如果我保存了一个名为categoreyId = 1的新组织,该组织指向学校categorey,则组织表中的CategoreyId将为= 2,而在类别表中将添加一个新ID = 2且其值为null,因此我需要解决这个问题的方法

这是我的组织课程:

    public int Id { get; set; }
    public string ownerFirstName { get; set; }
    public string ownerLastName { get; set; }
    public string orgnizationName { get; set; }
    public string websiteUrl { get; set; }
    public string Fax { get; set; }

    //Model Category 

    public Category Category { get; set; }
    public int categoryId { get; set; }

}

这是我的类别课程:

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Sector Sector { get; set; }
    public int sectorId { get; set; }
}

在控制器中:

  public ActionResult SignUp_Org()
        {
            var sectors = _context.Sector.ToList();
            var categories = _context.Categories.ToList(); 
            var viewmodel = new OrganizationViewModel {
            Sectors = sectors,
            Categories = categories,

            };
            return View(viewmodel);
    }

在我看来:

 @Html.DropDownListFor(o => o.Organization.categoryId,
 new SelectList(Model.Categories, "Id", "Name"), "Select Your Category",new {  @style = "width: 350px;padding:5px;border-radius: 3px;border: 2px solid #ccc;"})

0 个答案:

没有答案