1

I have a problem (some kind of "two-faced" because in first example code works, in second - not). When I use Create function for Category, field "RootCategory" autofill by Category-object with RootCategoryId (or null, if it's needed). That's work exactly how I want and how it must be!

BUT!

In the latter case, with Service and Create for it, object "Category" is always null! I think, it must work the same, but it's not!

Where is a mistake? Help me! There is somethink about FK (in Category FK => Category PK), (in Service FK => Category PK), but looks like there must be no diffrence for code.

I have two models for Entity (Code-first):

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

    public int? RootCategoryId { get; set; }
    public virtual Category RootCategory { get; set; }
}

And

public class Service
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Cost { get; set; }
    public string Comment { get; set; }

    public int? CategoryId { get; set; }
    public virtual Category Category { get; set; }
}

Context is:

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

}

Let's look at Create functions. For Category:

  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");
    }

For Service:

 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");
    }

And partials:

@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>
}

And

@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>
}
V.R.
  • 11
  • 2

0 Answers0