asp net mvc - 如何使用外键插入数据?

问题描述 投票:1回答:2

我是asp net mvc的新手,所以也许这很容易..我有一个包含类别的表和一个包含子类的表。子类别具有外键CategoryId。我的目标是当我在子类别的创建表单上时,选择一个类别来插入它。

这些是我的模型类:

 public class Category
 {
    [Key]
    public int CategoryId { get; set; }

    [Required]
    public string CategoryName { get; set; }
 }


 public class SubCategory
 {
    [Key]
    public int SubCategoryId { get; set; }
    [Required]
    public string SubCategoryName { get; set; }

    // Foreign key 
    [Display(Name = "Category")]
    public int CategoryId { get; set; }

    [ForeignKey("CategoryId")]
    public virtual Category Categories { get; set; }
}

我创建了存储库类,调用存储过程来插入数据。

在我的SubCategoryController中,我调用存储库类来插入它。

 public class SubCategoryController : Controller
{
    public ActionResult AddCat()
    {
        return View();
    }

    // POST: Employee/Create
    [HttpPost]
    public ActionResult AddCat(SubCategory Cat)
    {
        try
        {
            if (ModelState.IsValid)
            {
                SubCatRep SubCatRep = new SubCatRep();
                if (SubCatRep.AddCat(Cat))
                {
                    ViewBag.Message = "SubCat added!";
                }

            }
            return View();
        }
        catch
        {
            return View();
        }

    }
}

我创建了一个普通的创建视图。但是,当我启动我的应用程序时,CategoryID字段是数字字段..它没有给我一个选项来从类别表中选择dropdopwn或类似的东西。

<div class="form-horizontal">
    <h4>SubCategory</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.SubCategoryName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.SubCategoryName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.SubCategoryName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CategoryId, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

我是否需要创建包含类别的下拉列表?以及如何连接?我找到了一些例子,说它会因为模型类而手工制作。 this is the link to that

c# asp.net-mvc foreign-keys
2个回答
1
投票

试试吧。

<div class="form-group"> @Html.LabelFor(model => model.CategoryId,"CategoryId", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("CategoryId",null,htmlAttributes = new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })</div> </div>

1
投票

创建类别和子类别的视图模型,然后从该模型创建视图。例如

public class SubCategoryVM
{
public Subcategory subcategory {get;set;}
public List<Category > category {get;set;}
}
© www.soinside.com 2019 - 2024. All rights reserved.