简体   繁体   English

ASP Net MVC-如何使用外键插入数据?

[英]asp net mvc - How to insert data with the foreign key?

I am new to the asp net mvc, so maybe this is easy.. I have a table with categories and a table with subcategories. 我是ASP Net MVC的新手,所以也许这很容易。我有一个带有类别的表和一个带有子类别的表。 Subcategories have the foreign key CategoryId. 子类别具有外键CategoryId。 My goal is to when I am on a create form for the subcategory, to choose one category to insert it. 我的目标是当我在创建子类别的表单时,选择一个类别来插入它。

These are my model classes: 这些是我的模型类:

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

I have created repository classes that are calling stored procedures for inserting data. 我创建了存储库类,这些存储库类正在调用存储过程以插入数据。

And in my SubCategoryController, I am calling the repository class to insert it. 在我的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();
        }

    }
}

I have created a normal create view. 我已经创建了一个普通的创建视图。 But when I start my app, the CategoryID field is the number field..It does not give me an option to choose a dropdopwn or something like that from the Category table. 但是,当我启动我的应用程序时,CategoryID字段是数字字段。它没有让我选择从Category表中选择下拉菜单或类似内容的选项。

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

Do I need to create a dropdown list with the categories? 我需要创建带有类别的下拉列表吗? And how to connect that? 以及如何连接呢? I have found some example that says that it would manualy do that because of the model classes. 我发现了一些示例,该示例由于模型类而将手动执行此操作。 this is the link to that 这是那个的链接

这样尝试。

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

Create a View model of category and sub category and then create a view from that model. 创建类别和子类别的视图模型,然后从该模型创建视图。 Eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM