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 9072627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:12:54+00:00 2026-06-16T18:12:54+00:00

In my mvc 3 web store app I have this model: Items, Products, Shipping,

  • 0

In my mvc 3 web store app I have this model: Items, Products, Shipping, Users tables, where 1 Item contains 1 Product, 1 User, and 1 Shipping. I need to somehow create and edit items. With creating there are not many problems cause I can pass the “fields to fill” as parameters to Crete(Post) method. With editing there is one problem – Null Reference Exception occures. This is my code:
(controller):
I pass model (of type Item) to Edit Post method as a parameter and get product and shipping, that, I hope), I filled with values.

    [HttpGet]
    public ViewResult Edit(int itemId)
    {
        Item target = _itemsRepository.GetItem(itemId);

        return View(target);
    }

    [HttpPost]
    public ActionResult Edit(Item model)
    {
        Product p = model.Product;
        Shipping s = model.Shipping;

        // here goes some validation stuff

        if (ModelState.IsValid)
        {
            _productRepository.UpdateProduct(p);
            _shippingRepository.UpdateShipping(s);
            return RedirectToAction("Content");
        }
        return View();
    }    

Strongly typed view (where I fill the form):

@model WebStore.WebStoreModels.Item

@using (Html.BeginForm())
{
@Html.ValidationSummary(true)

<fieldset>
    <legend>Product</legend>
    @Html.HiddenFor(i => i.ItemId)

    <p>Name: </p>
    <p>
        @Html.EditorFor(i => i.Product.Name)
        @Html.ValidationMessageFor(i => i.Product.Name)
    </p>
   // and so on
</fieldset>

<fieldset>
    <legend>Shipping</legend>

    <p>Cost: </p>
    <p>
        @Html.EditorFor(i => i.Shipping.Cost)
        @Html.ValidationMessageFor(i => i.Shipping.Cost)
    </p>        
    // so on
</fieldset> 
<p>
    <input type="submit" value="Save"/>
</p>

}

And when I fill the form and click “safe” button the NullReferenceException occures in the ProductRepository class in UpdateProduct() method:

public class ProductRepository : IProductRepository
{
    private WebStoreDataContext _dataContext;

    public ProductRepository(WebStoreDataContext dataContext)
    {
        _dataContext = dataContext;
    }

    public Product GetProduct(int productId)
    {
        return _dataContext.Products.SingleOrDefault(p => p.ProductId == productId);
    }
    public void UpdateProduct(Product p)
    {
        var dbProduct = GetProduct(p.ProductId);
        dbProduct.Name = p.Name;   // here the exception occures
        dbProduct.Description = p.Description;
        dbProduct.Price = p.Price;
        dbProduct.Category = p.Category;
        dbProduct.SubCategory = p.SubCategory;
        _dataContext.SubmitChanges();
    }

Seems like I can’t use this assignment:

Product p = model.Product;

I also tried: (in view and then assign it to Product in Edit(Post) method)

TempData["product"] = @Model.Product;  

And also: (in view)

@Html.Hidden("product", @Model.Product)  

And pass it as parameters to Edit(Post) method:

[HttpGet]
public ViewResult Edit(Product p, Shipping s)  

I think the problem is associated with model.
Any help would be great, Sorry for so much code)

  • 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-16T18:12:55+00:00Added an answer on June 16, 2026 at 6:12 pm

    You need to add hidden ProductId input inside of the form in your View:

    @using (Html.BeginForm())
    {
        @Html.HiddenFor(i => i.Product.ProductId)
        ...
    }
    

    The reason you need to have this line is that when you submit your form model binder looks at every input (including hidden inputs) and binds them to the properties of your Item model that you pass on [HttpPost] action. Before, when you didn’t have this line, only the following properties were populated:

    • Item.ItemId
    • Item.Product.Name
    • Item.Shipping.Cost

    Your model didn’t have information for the value of Item.Product.ProductId. This property is int, which means that it was turning to be equal 0 when form was submitted. Inside your _productRepository.UpdateProduct(p); method you are trying to get a product by Id and obviously it cannot find a product with id = 0, which is why this call was returning null resulting in a null reference exception on the next line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my web store mvc app I want to add items to database. Items
Background I have a page on my ASP.NET MVC web app for users to
I have C# MVC web app that has some textboxes that in IE9 you
I have browsed and uploaded a png/jpg file in my MVC web app. I
I have an asp.net mvc app with a route that allows users to request
I have an ASP.NET MVC web app which requires the user to login with
I'm building a web app using EF Code First and ASP.NET MVC. I have
We have an MVC web app that uses FormsAuthentication and also stores a couple
On my web site (asp.net mvc), I use cookie to store search pattern. when
I have a mvc web project where I try to render a list of

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.