Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有ViewModel MVC4的FK约束_C#_Asp.net Mvc_Viewmodel - Fatal编程技术网

C# 具有ViewModel MVC4的FK约束

C# 具有ViewModel MVC4的FK约束,c#,asp.net-mvc,viewmodel,C#,Asp.net Mvc,Viewmodel,我在使用MVC4和实体框架时遇到了一些问题。我有一个实体“Person”,它由另一个名为“ProductPackageCategories”的实体组成。我还有一个ViewModel“PersonViewModel”。在“CreatePerson”视图中,我可以创建我的新Person并使用dropdownlist组件指定类别。通常,当我单击submit按钮时,数据存储在数据库中。在我的例子中,我检索所有数据,但不检索ProductPackageCategory Id。有人知道这个问题吗 异常消息:

我在使用MVC4和实体框架时遇到了一些问题。我有一个实体“Person”,它由另一个名为“ProductPackageCategories”的实体组成。我还有一个ViewModel“PersonViewModel”。在“CreatePerson”视图中,我可以创建我的新Person并使用dropdownlist组件指定类别。通常,当我单击submit按钮时,数据存储在数据库中。在我的例子中,我检索所有数据,但不检索ProductPackageCategory Id。有人知道这个问题吗

异常消息:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_bm_Persons_bm_ProductPackageCategories". The conflict occurred in database "C:\USERS\MAARAB\DESKTOP\PROJECT\2013-03-01_4-9_MA-OS_BUSI MATERIAL PROJECT\BUSIMATERIAL\BUSIMATERIAL\APP_DATA\BUSIMATERIAL.MDF", table "dbo.bm_ProductPackageCategories", column 'Id_ProductPackageCategory'.
声明已终止

我在PersonController中的创建操作:

public ActionResult Create()
        {
            ViewBag.Id_ProductPackageCategory = new SelectList(db.ProductPackageCategories, "Id_ProductPackageCategory", "Name");
            return View();
        }

        //
        // POST: /Person/Create

        [HttpPost]
        public ActionResult Create(PersonViewModel personViewModel)
        {

            if (ModelState.IsValid)
            {

                db.Persons.AddObject(personViewModel.person);
                db.SaveChanges();
                return RedirectToAction("Index");
            }



            ViewBag.Id_ProductPackageCategory = new SelectList(db.ProductPackageCategories, "Id_ProductPackageCategory", "Name", personViewModel.person.Id_ProductPackageCategory);

            return View(personViewModel);
        }
我的ViewModel:

public class PersonViewModel
{
    public Person person { get; set; }

    public PersonViewModel()
    {
        person = new Person();
    }

    public PersonViewModel(Person person)
    {
        this.person = person;
    }

}
我的创建视图:

@model BuSIMaterial.Models.PersonViewModel

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Person</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.person.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.person.FirstName)
            @Html.ValidationMessageFor(model => model.person.FirstName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.person.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.person.LastName)
            @Html.ValidationMessageFor(model => model.person.LastName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.person.NumNat)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.person.NumNat)
            @Html.ValidationMessageFor(model => model.person.NumNat)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.person.StartDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.person.StartDate)
            @Html.ValidationMessageFor(model => model.person.StartDate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.person.EndDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.person.EndDate)
            @Html.ValidationMessageFor(model => model.person.EndDate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.person.Upgrade)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.person.Upgrade)
            @Html.ValidationMessageFor(model => model.person.Upgrade)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.person.Id_ProductPackageCategory, "Category")
        </div>
        <div class="editor-field">
            @Html.DropDownList("Id_ProductPackageCategory", "Choose one...")
            @Html.ValidationMessageFor(model => model.person.Id_ProductPackageCategory)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.person.HouseToWorkKilometers)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.person.HouseToWorkKilometers)
            @Html.ValidationMessageFor(model => model.person.HouseToWorkKilometers)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
@model.material.Models.PersonViewModel
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
人
@LabelFor(model=>model.person.FirstName)
@EditorFor(model=>model.person.FirstName)
@Html.ValidationMessageFor(model=>model.person.FirstName)
@LabelFor(model=>model.person.LastName)
@EditorFor(model=>model.person.LastName)
@Html.ValidationMessageFor(model=>model.person.LastName)
@LabelFor(model=>model.person.NumNat)
@EditorFor(model=>model.person.NumNat)
@Html.ValidationMessageFor(model=>model.person.NumNat)
@LabelFor(model=>model.person.StartDate)
@EditorFor(model=>model.person.StartDate)
@Html.ValidationMessageFor(model=>model.person.StartDate)
@LabelFor(model=>model.person.EndDate)
@EditorFor(model=>model.person.EndDate)
@Html.ValidationMessageFor(model=>model.person.EndDate)
@LabelFor(model=>model.person.Upgrade)
@EditorFor(model=>model.person.Upgrade)
@Html.ValidationMessageFor(model=>model.person.Upgrade)
@Html.LabelFor(model=>model.person.Id\u ProductPackageCategory,“Category”)
@Html.DropDownList(“Id\u ProductPackageCategory”,“选择一个…”)
@Html.ValidationMessageFor(model=>model.person.Id\u ProductPackageCategory)
@LabelFor(model=>model.person.housetoworkkm)
@EditorFor(model=>model.person.housetoworkkm)
@Html.ValidationMessageFor(model=>model.person.houseToworkkm)

} @ActionLink(“返回列表”、“索引”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }
更换:

@Html.DropDownList("Id_ProductPackageCategory", "Choose one...")
与:


这样,当表单提交到HttpPost操作时,模型上的
person.Id\u ProductPackageCategory
属性将从下拉列表中的选定值正确绑定。

使用强类型html帮助程序将属性绑定到模型。使用
DropDownListFor
代替
DropDownList

控制器

public ActionResult Create()
{
    ViewBag.Id_ProductPackageCategory = new SelectList(db.ProductPackageCategories, "Id_ProductPackageCategory", "Name");
    return View();
}
看法


谢谢你的帮助!只是一个小问题,对于“model=>model.person.Id\u ProductPackageCategory”部分,VS显示了一个错误,即“无法将lambda表达式转换为类型'string',因为它不是委托类型”…请确保使用System.Data添加
使用System.Data.Entity添加
,使用System.Linq添加
我的错误,我忘记修改“DropDownList for”中的“DropDownList”,谢谢!再次感谢你的帮助!只是一个小问题,对于“model=>model.person.Id\u ProductPackageCategory”部分,VS显示了一个错误,即“无法将lambda表达式转换为类型'string',因为它不是委托类型”…我的错误,我忘了在“DropdownListFor”中修改“DropDownList”,谢谢!
public ActionResult Create()
{
    ViewBag.Id_ProductPackageCategory = new SelectList(db.ProductPackageCategories, "Id_ProductPackageCategory", "Name");
    return View();
}
@Html.DropDownListFor(model => model.person.Id_ProductPackageCategory, ViewBag.Id_ProductPackageCategory as SelectList, "--- Select Category ---", new { @class = "some_class" })