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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:26:04+00:00 2026-06-08T23:26:04+00:00

I’ve used a ViewModel to represent two entities in my database, Categories & Manufacturers,

  • 0

I’ve used a ViewModel to represent two entities in my database, “Categories” & “Manufacturers”, because I want them as drop down list.
I’ve managed to create sound and working drop down lists of them both.

Here’s the ViewModel:

 public class New_Product_View_Model
  {
    public int SelectedManufacturerID { get; set; }
    public SelectList Manufacturers { get; set; }
    public int SelectedCategoryID { get; set; }
    public SelectList Categories { get; set; }
  }

Here’s the [HttpGet] Controller Action:

public ActionResult Insert_New_Product()
   {
        var dbcontext = new LNQ2SQLDataContext();
        var CatQ = from P in dbcontext.Categories
                where P.SUB_CAT == null
                select P;

        var ManQ = from P in dbcontext.Manufacturers
                   select P;

        var VM = new New_Product_View_Model();
        VM.Categories = new SelectList(CatQ, "CAT_ID", "CAT_Name");
        VM.Manufacturers = new SelectList(ManQ, "MAN_ID", "MAN_Name");

      //Passes the 'VM' to the view
        return View(VM);
   }

AND the View I have looks like this :

@model MyAppName.ViewModels.New_Product_View_Model
@using (Html.BeginForm(FormMethod.Post))
    {
        @Html.DropDownListFor(m => m.SelectedCategoryID, Model.Categories, "-- Select a category --")
        <br />
        @Html.DropDownListFor(m => m.SelectedManufacturerID, Model.Manufacturers, "-- Select a manufacturer --")

    <input type="submit" value="Add New Product" />

    }

Everything work absolutely fine so far

Now here’s the [HttpPost] ActionResult :

[HttpPost]
public ActionResult Insert_New_Product(New_Product_View_Model NPVM)
    {
      //A few things here
      //then
        return View();
    }

When I click the submit button in the page, I get this error:

See the picture here

And as far as I know, it’s because the Model in the view is null when the HttpPost method runs. Because the return in the HttpPost action, doesn’t have an instance of New_Product_View_Model.
It’s silly because I don’t want to pass any models to the view AGAIN!
I just want the same page to be available in case I need to add more products.

I tried to exchange the return with this one:

return View(new New_Product_View_Model());

But I get this error:
See the picture here

I also tried this:

NPVM = new New_Product_View_Model();
return View(NPVM);

But just got the same error.

Does anyone have any solution ?
I’d really appreciate any help.

  • 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-08T23:26:06+00:00Added an answer on June 8, 2026 at 11:26 pm

    HTTP is stateless and MVC is a beautyful example of a being a Statless Loving web development framework. each request is like new. So you should repopulate the Collection properties when you send the model back to the view.

    Consider using the PRG (Post-Redirect-GET) pattern. That means , If your transaction (Save to DB) is successful, you should do a Redirect to a Get action to show the new product

    [HttpPost]
    public ActionResult Insert_New_Product(New_Product_View_Model model)
    {
      if(ModelState.IsValid)
      {     
    
        //Save & If everything is fine, Do A Redirect (PRG pattern) to Get action
        return RedirectToAction("ShowProduct",new { id=model.ID});
      }
    
       //Model validation failed. Reload the stuff and send back to view     
    
        model.Categories = GetCategories();
        model.Manufacturers = GetManufacturers ();
    
        return View(model);
    }
    
    public ActionResult ShowProduct(int id)
    {
      var item=repo.GetItemFRomId(id);
      return View(item);
    }
    

    Assuming GetCategories() and GetManufacturers method returns a SelectList of Your Categories and Manufacturers, which you can use to bind the drop downs in your view.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I used javascript for loading a picture on my website depending on which small
I want to count how many characters a certain string has in PHP, but
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
i want to parse a xhtml file and display in UITableView. what is the
I want to construct a data frame in an Rcpp function, but when I

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.