1

我在 asp.net mvc 中保存 master 的详细信息时遇到问题。

作为参考,我正在使用 nhibernate。

我在 Store 和 Employee 实体之间有一对多的关系。我分两步保存主文件和细节。您首先创建商店,保存它,然后创建员工。

这是两个类:

public class Store
{
    public Store()
    {
        Employees = new List<Employee>();
    }

    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Employee> Employees { get; set; }
}

public class Employee
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Store Store { get; set; }
}

因此,要创建商店,我在商店的控制器和视图中有以下代码:

public virtual ActionResult Create()
{
    var model = new StoreModel();

    return View(model);
}

[HttpPost]
public ActionResult Create(StoreModel model)
{
    if (ModelState.IsValid)
    {
        Store entity = new Store(model);

        Repository<Store> repository = new Repository<Store>();
        repository.Create(entity);
        return RedirectToAction("Index");
    }

    return View(model);
}


@model StoreModel
@{
    ViewBag.Title = "Create store";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
    Create store
</h2>
@Html.ValidationSummary(true)
@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(store => store.Name)
    </div>
    <div>
        @Html.TextBoxFor(store => store.Name)
        @Html.ValidationMessageFor(store => store.Name)
    </div>
    <p>
        <input type="submit" value="Create"/>
    </p>
}
<div>
    @Html.ActionLink("Back", "Index")
</div>

这很好,但我的问题是当我试图拯救员工时,商店总是 null。因此,要创建员工,我在员工的控制器和视图中有以下代码:

public virtual ActionResult Create(int storeId)
{
    var model = new EmployeeModel();
    Repository<Store> repository = new Repository<Store>();
    model.Store = repository.Read(storeId);

    return View(model);
}

[HttpPost]
public ActionResult Create(EmployeeModel model) //Problem here, store is null in the model
{
    if (ModelState.IsValid)
    {
        Employee entity = new Employee(model);

        Repository<Employee> repository = new Repository<Employee>();
        repository.Create(entity);
        return RedirectToAction("Index");
    }

    return View(model);
}

@model EmployeeModel
@{
    ViewBag.Title = "Create employee";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
    Create employee
</h2>
@Html.ValidationSummary(true)
@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(employee => employee.Name)
    </div>
    <div>
        @Html.TextBoxFor(employee => employee.Name)
        @Html.ValidationMessageFor(employee => employee.Name)
    </div>
    <p>
        <input type="submit" value="Create"/>
    </p>
}
<div>
    @Html.ActionLink("Back", "Index")
</div>

我究竟做错了什么?

4

3 回答 3

1

您没有在任何地方设置员工的商店财产。

于 2012-07-05T23:02:30.507 回答
0

您没有在任何地方传递 storeId 。我建议您在“创建员工”表单中创建一个下拉列表,并用您的商店列表填充它。

于 2012-07-06T00:19:36.187 回答
0

我能够通过在 httppost 创建中添加 int storeId 并从数据库中读取存储并设置员工的存储来解决我的问题。

[HttpPost]
public ActionResult Create(EmployeeModel model, int storeId) //Problem here, store is null in the model
{
    if (ModelState.IsValid)
    {
        Repository<Store> storeRepository = new Repository<Store>();    
        Store store = storeRepository.Read(storeId);

        Employee employee = new Employee(model);
        employee.Store = store;

        Repository<Employee> employeeRepository = new Repository<Employee>();
        employeeRepository.Create(employee);
        return RedirectToAction("Index");
    }

    return View(model);
}

并使用隐藏字段:

@model EmployeeModel
@{
    ViewBag.Title = "Create employee";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
    Create employee
</h2>
@Html.ValidationSummary(true)
@using (Html.BeginForm())
{
    @Html.Hidden("storeId", Model.Store.Id)
    <div>
        @Html.LabelFor(employee => employee.Name)
    </div>
    <div>
        @Html.TextBoxFor(employee => employee.Name)
        @Html.ValidationMessageFor(employee => employee.Name)
    </div>
    <p>
        <input type="submit" value="Create"/>
    </p>
}
<div>
    @Html.ActionLink("Back", "Index")
</div>
于 2012-07-06T21:40:24.263 回答