SiriusNik
SiriusNik

Reputation: 571

Asp.net mvc save One-to-many and passing master on submit

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?

Upvotes: 1

Views: 1608

Answers (3)

SiriusNik
SiriusNik

Reputation: 571

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.

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

and using a hidden field:

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

Upvotes: 0

Sardonic
Sardonic

Reputation: 126

You're not passing the storeId anywhere. I suggest you create a dropdown in your 'Create Employee' form and populate it with your list of stores.

Upvotes: 0

BlackICE
BlackICE

Reputation: 8926

You're not setting the employee's store property anywhere.

Upvotes: 1

Related Questions