Asp.net mvc保存一对多并在提交时传递主控
在asp.net mvc中保存主机的详细信息时遇到问题 作为参考,我使用nhibernate 我在商店和员工实体之间有一对多的关系。 我通过两个步骤保存主视图和详细信息。 首先创建商店,保存它,然后创建员工 以下是两个课程:Asp.net mvc保存一对多并在提交时传递主控,asp.net,asp.net-mvc,nhibernate,razor,one-to-many,Asp.net,Asp.net Mvc,Nhibernate,Razor,One To Many,在asp.net mvc中保存主机的详细信息时遇到问题 作为参考,我使用nhibernate 我在商店和员工实体之间有一对多的关系。 我通过两个步骤保存主视图和详细信息。 首先创建商店,保存它,然后创建员工 以下是两个课程: public class Store { public Store() { Employees = new List<Employee>(); } public virtual int Id { get; set
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; }
}
公共类存储
{
公共商店()
{
雇员=新名单();
}
公共虚拟整数Id{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟IList雇员{get;set;}
}
公营雇员
{
公共虚拟整数Id{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟存储{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>
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>
公共虚拟操作结果创建()
{
var模型=新的StoreModel();
返回视图(模型);
}
[HttpPost]
公共操作结果创建(StoreModel)
{
if(ModelState.IsValid)
{
门店实体=新门店(模型);
Repository Repository=新存储库();
创建(实体);
返回操作(“索引”);
}
返回视图(模型);
}
@模型库模型
@{
ViewBag.Title=“创建商店”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
创建存储
@Html.ValidationSummary(true)
@使用(Html.BeginForm())
{
@LabelFor(store=>store.Name)
@Html.TextBoxFor(store=>store.Name)
@Html.ValidationMessageFor(store=>store.Name)
}
@ActionLink(“返回”、“索引”)
这很好,但我的问题是,当我试图拯救员工时,存储总是空的。
因此,要创建员工,我在员工的控制器和视图中有以下代码:
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>
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>
公共虚拟操作结果创建(int-storeId)
{
var模型=新员工模型();
Repository Repository=新存储库();
model.Store=repository.Read(storeId);
返回视图(模型);
}
[HttpPost]
public ActionResult Create(EmployeeModel模型)//这里有问题,模型中的存储为null
{
if(ModelState.IsValid)
{
员工实体=新员工(模型);
Repository Repository=新存储库();
创建(实体);
返回操作(“索引”);
}
返回视图(模型);
}
@模型雇员模型
@{
ViewBag.Title=“创建员工”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
创建员工
@Html.ValidationSummary(true)
@使用(Html.BeginForm())
{
@LabelFor(employee=>employee.Name)
@Html.TextBoxFor(employee=>employee.Name)
@Html.ValidationMessageFor(employee=>employee.Name)
}
@ActionLink(“返回”、“索引”)
我做错了什么?您没有在任何地方设置员工的门店属性。您没有在任何地方设置员工的门店属性。您没有在任何地方传递门店ID。我建议您在“创建员工”表单中创建一个下拉列表,并用您的门店列表填充它。您不会在任何地方传递storeId。我建议您在“创建员工”表单中创建一个下拉列表,并用您的门店列表填充它。我可以通过在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);
}
[HttpPost]
public ActionResult Create(EmployeeModel,int-storeId)//这里有个问题,模型中的store为null
{
if(ModelState.IsValid)
{
Repository storeRepository=新存储库();
Store Store=storeRepository.Read(storeId);
员工=新员工(模型);
employee.Store=Store;
Repository employeeRepository=新存储库();
employeeRepository.Create(employee);
返回操作(“索引”);
}
返回视图(模型);
}
并使用隐藏字段:
@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>
@model EmployeeModel
@{
ViewBag.Title=“创建员工”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
创建员工
@Html.ValidationSummary(true)
@使用(Html.BeginForm())
{
@隐藏(“storeId”,Model.Store.Id)
@LabelFor(employee=>employee.Name)
@Html.TextBoxFor(employee=>employee.Name)
@Html.ValidationMessageFor(employee=>employee.Name)
}
@ActionLink(“返回”、“索引”)
我可以通过在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);
}
[HttpPost]
public ActionResult Create(EmployeeModel,int-storeId)//这里有个问题,模型中的store为null
{
if(ModelState.IsValid)
{
Repository storeRepository=新存储库();
Store Store=storeRepository.Read(storeId);
员工=新员工(模型);
employee.Store=Store;
Repository employeeRepository=新存储库();
employeeRepository.Create(employee);
返回操作(“索引”);
}
返回视图(模型);
}
并使用隐藏字段:
@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>
@model EmployeeModel
@{
ViewBag.Title=“创建员工”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
创建员工
@Html.ValidationSummary(true)
@使用(Html.BeginForm())
{
@隐藏(“storeId”,Model.Store.Id)
@LabelFor(employee=>employee.Name)
@Html.TextBoxFor(employee=>employee.Name)
@Html.ValidationMessageFor(employee=>employee.Name)
}
@ActionLink(“返回”、“索引”)
这里我做了一些修改,我在员工控制器的创建中设置了存储。我看到您在创建(int storeid)函数中这样做,但在创建(EmployeeModel)函数中没有这样做,