0

I'm making a webshop and when I add products everything is going alright, when I try to edit my products I get an error on the category they are under.

this is my code:

@model WorkshopASPNETMVC_III_Start.ViewModels.ProductViewModel

//somecode

{
//view other text boxes
    <div class="editor-field">
        @Html.DropDownListFor(model => model.SelectedSubcatID, Model.Subcats)
        @Html.ValidationMessageFor(model => model.SelectedSubcatID)       
    </div>

Now this error occurs when I actually change them, not when I get the view to change em. Its says my selectedSubcatID or Model.Subcats are null.

but not one of them is:

ProductViewModel viewModel = new ProductViewModel();
Product product = productDBController.getProduct(productId);
viewModel.Product = product;
viewModel.SelectedSubcatID = product.Subcat.subcat_id;
viewModel.Subcats = getSelectListSubcats();
return View(viewModel);


private SelectList getSelectListSubcats()
{
    List<SubCategorie> subcats = subcatDBController.GetSubCats();
    SubCategorie emptySubcat = new SubCategorie();
    emptySubcat.subcat_id = -1;
    emptySubcat.naam = "";
    subcats.Insert(0, emptySubcat);

    return new SelectList(subcats, "subcat_id", "Naam");
}

EDIT

[HttpPost]
public ActionResult WijzigProduct(ProductViewModel viewModel, HttpPostedFileBase file)
{
    if (file == null)
    {
        MessageBox.Show("Product niet gewijzigd, geen plaatje geselecteerd!");
        return RedirectToAction("NieuwProduct", "Beheer");
    }
    else
    {
        String path = "/Content/Images";
        DirectoryInfo info = new DirectoryInfo(Server.MapPath(path));
        if (!info.Exists)
        {
            info.Create();
        }

        String fullName = String.Format("/Content/Images/{0}", file.FileName);
        file.SaveAs(Server.MapPath(fullName));

        try
        {
            if (ModelState.IsValid)
            {
                viewModel.Product.Subcat = subcatDBController.getSubCat(viewModel.SelectedSubcatID);
                productDBController.UpdateProduct(viewModel.Product, fullName);
                return RedirectToAction("beheerPagina", "Beheer");
            }
            else
            {
                viewModel.Subcats = getSelectListSubcats();
                return View(viewModel);
            }

        }
        catch (Exception e)
        {
            ViewBag.FoutMelding = "Er is iets fout gegaan: " + e;
            return View();
        }
    }
}
John Saunders
  • 157,405
  • 24
  • 229
  • 388
Kevin
  • 291
  • 4
  • 15
  • You say it happens when you change your selection. Are you submitting your form to see the error? – Michael La Voie Apr 17 '13 at 00:26
  • Yeah when I put in the new info and click 'edit' my nullreferencexcetion, object not sent to instance of exception occurs on this: @Html.DropDownListFor(model => model.SelectedSubcatID, Model.Subcats) – Kevin Apr 17 '13 at 00:28
  • Can you show the controller POST action for saving? My guess is that you might need to call `getSelectListSubcats` in that action. – rikitikitik Apr 17 '13 at 00:29
  • sure i'll edit orginal post. – Kevin Apr 17 '13 at 00:30
  • Hmm, can you debug through the post back and let us know which code path it returns on (the isvalid path, !isvalid path, the catch path, the file==null path? – Michael La Voie Apr 17 '13 at 00:36
  • It is going in the isvalid, like it should .. – Kevin Apr 17 '13 at 00:46
  • Honestly, I'm still at a loss for where the error is coming from. If you have an error on the post back, why don't you know which control is null? Can you show the stack trace? Or better yet, the exact line of code that throws the error? Otherwise, if you get the error on a view, then it must be this view: RedirectToAction("beheerPagina", "Beheer") – Michael La Voie Apr 17 '13 at 00:54
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Apr 17 '13 at 00:55
  • this might help: in my debugger window it says :A first chance exception of type 'System.FormatException' occurred in mscorlib.dll A first chance exception of type 'System.FormatException' occurred in WorkshopASPNETMVC(II)Start.DLL A first chance exception of type 'System.NullReferenceException' occurred in App_Web_5wu5a0ke.dll – Kevin Apr 17 '13 at 00:58

1 Answers1

0

I found the problem, not sure what is happening but it might be worth checking out when you run upon this problem one day.

in my file where I put all the new attributes in, such as price, name etc. I have validations in the model. one validation on price was not correct. I commented it out and It worked:

check validations on this:

@model WorkshopASPNETMVC_III_Start.ViewModels.ProductViewModel
//somecode
{
//view other text boxes
<div class="editor-field">
    @Html.DropDownListFor(model => model.SelectedSubcatID, Model.Subcats)
    @Html.ValidationMessageFor(model => model.SelectedSubcatID)       
</div>

Im not sure why Im getting this nullreferenceexception but I'm really glad it got fixed! thanks for the support guys.

Kevin
  • 291
  • 4
  • 15