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

时间:2018-02-22 15:30:37

标签: c# asp.net-mvc foreign-keys

我是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

2 个答案:

答案 0 :(得分: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 :(得分:1)

创建类别和子类别的视图模型,然后从该模型创建视图。 E.g

public class SubCategoryVM
{
public Subcategory subcategory {get;set;}
public List<Category > category {get;set;}
}