Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8194779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:56:06+00:00 2026-06-07T04:56:06+00:00

I have a problem saving the detail of a master in asp.net mvc. For

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:56:07+00:00Added an answer on June 7, 2026 at 4:56 am

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using CakePHP framework with MySQL database and I have problem in saving
I have a few asp.net web applications running on a dedicated server.. For one
I've been having a problem regarding using AJAX with Spring MVC. I have a
I am using ASP.net(2.0) with VB.NET. I have a User registration form. On that
I have 2 tables with a master-detail relationship. We use a DataGridView to handle
Hi I have a problem saving 2 tables of my database at the same
I have a problem with saving the path in doctrine from a file upload.
I have problem with my query on C, I’m using the oci8 driver. This
I have a Property table and another is Detail table. Where I am using
I have a problem with making changes to ManagedObjects and saving those changes to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.