Asp.net 实体:ForeignKey对象始终为空

Asp.net 实体:ForeignKey对象始终为空,asp.net,asp.net-mvc,entity-framework,entity,crud,Asp.net,Asp.net Mvc,Entity Framework,Entity,Crud,我有一个问题(某种“两面派”,因为在第一个示例中代码有效,而在第二个示例中代码无效)。当我为Category使用Create函数时,字段“RootCategory”会按类别对象自动填充RootCategoryId(如果需要,也可以为null)这就是工作正是我想要的和必须的 但是 在后一种情况下,使用Service和Create for it时,对象“Category”始终为空!我想,它一定是一样的,但它不是 哪里有错误帮帮我有一些关于FK的想法(在类别FK=>Category PK中),(在服务

我有一个问题(某种“两面派”,因为在第一个示例中代码有效,而在第二个示例中代码无效)。当我为Category使用Create函数时,字段“RootCategory”会按类别对象自动填充RootCategoryId(如果需要,也可以为null)这就是工作正是我想要的和必须的

但是

在后一种情况下,使用Service和Create for it时,对象“Category”始终为空!我想,它一定是一样的,但它不是

哪里有错误帮帮我有一些关于FK的想法(在类别FK=>Category PK中),(在服务FK=>Category PK中),但是看起来代码一定没有区别

我有两个实体模型(代码优先):

背景是:

public class SiteContext : DbContext
{
    ...
    public DbSet<Service> Services { get; set; }
    public DbSet<Category> Categories { get; set; }
    ...

}
服务:

 public ActionResult Create()
    {
        SelectList ct = new SelectList(db.Categories, "Id", "Name");
        ViewBag.Categ = ct;
        return PartialView("Create");
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Service service)
    {
        db.Services.Add(service);
        db.SaveChanges();
        return RedirectToAction("Index", "Admin");
    }
和部分:

@model DialService.Models.Category

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.Id)
    <div>
        @Html.LabelFor(model => model.Name)
        <p>@Html.EditorFor(model => model.Name)</p>
    </div>
    <div>
        @Html.LabelFor(model => model.RootCategoryId)
        <p>@Html.DropDownListFor(model => model.RootCategoryId, ViewBag.Cats as IEnumerable<SelectListItem>, string.Empty)</p>
    </div>
    <p><input type="submit" value="Добавить" /></p>
}
@model DialService.Models.Category
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model=>model.Id)
@LabelFor(model=>model.Name)
@EditorFor(model=>model.Name)

@LabelFor(model=>model.RootCategoryId) @Html.DropDownListFor(model=>model.RootCategoryId,ViewBag.Cats作为IEnumerable,string.Empty)

}

@model DialService.Models.Service
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model=>model.Id)
@LabelFor(model=>model.Name)
@EditorFor(model=>model.Name)

@LabelFor(model=>model.Cost) @EditorFor(model=>model.Cost)

@LabelFor(model=>model.Comment) @EditorFor(model=>model.Comment)

@LabelFor(model=>model.CategoryId) @Html.DropDownListFor(model=>model.CategoryId,ViewBag.Categ为IEnumerable)

}
  public ActionResult Create()
    {
        SelectList ct = new SelectList(db.Categories, "Id", "Name");
        ViewBag.Cats = ct;
        return PartialView("Create");
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Category category)
    {
        db.Categories.Add(category);
        db.SaveChanges();
        return RedirectToAction("Index", "Admin");
    }
 public ActionResult Create()
    {
        SelectList ct = new SelectList(db.Categories, "Id", "Name");
        ViewBag.Categ = ct;
        return PartialView("Create");
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Service service)
    {
        db.Services.Add(service);
        db.SaveChanges();
        return RedirectToAction("Index", "Admin");
    }
@model DialService.Models.Category

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.Id)
    <div>
        @Html.LabelFor(model => model.Name)
        <p>@Html.EditorFor(model => model.Name)</p>
    </div>
    <div>
        @Html.LabelFor(model => model.RootCategoryId)
        <p>@Html.DropDownListFor(model => model.RootCategoryId, ViewBag.Cats as IEnumerable<SelectListItem>, string.Empty)</p>
    </div>
    <p><input type="submit" value="Добавить" /></p>
}
@model DialService.Models.Service
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.Id)
    <div>
        @Html.LabelFor(model => model.Name)
        <p>@Html.EditorFor(model => model.Name)</p>
    </div>
    <div>
        @Html.LabelFor(model => model.Cost)
        <p>@Html.EditorFor(model => model.Cost)</p>
    </div>
    <div>
        @Html.LabelFor(model => model.Comment)
        <p>@Html.EditorFor(model => model.Comment)</p>
    </div>
    <div>
        @Html.LabelFor(model => model.CategoryId)
        <p>@Html.DropDownListFor(model => model.CategoryId, ViewBag.Categ as IEnumerable<SelectListItem>)</p>
    </div>
    <p><input type="submit" value="Добавить" /></p>
}