I have a problem saving the detail of a master in asp.net mvc.
For reference I am using nhibernate.
I have a One-to-many relationship between the Store and Employee entities.
I save the master and the detail in 2 steps.
You create the Store first, save it, then you create the employees.
Here are both classes:
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; }
}
So to create the Store I have the following code in the store’s controller and view:
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>
That works fine, but my problem is when i try to save the employees, the store is always null.
So to create the employees I have the following code in the employee’s controller and view:
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>
What am I doing wrong?
I was able to fix my problem by adding int storeId in the httppost create and read the store from the database and setting the emplyee’s store.
and using a hidden field: