locked
form validation with Html.DropDownList RRS feed

  • Question

  • User1994633106 posted

     Hi!

    I'm having a problem with validating a simple form for inserting products in a Linq To SQL database. It contains a few Html.TextBoxes and a single Html.DropDownList. Whenever I try to insert a product in the database, and I leave out one or more of the textfields I get the following error message, which apparently has something to do with my dropdownlist. The dropdownlist consists of all the categories in the database. I do not understand why I get this message, since the dropdownlist selection is not null?

     

    Edit: this actually happens even though I fill out all of the text fields. It seems to be a problem with my dropdownlist, because I did another test on a new document consisting only of a Html.dropdownlist. How do I create these lists correctly?

     Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentNullException: Value cannot be null.
    Parameter name: selectList

    Source Error:

    Line 63:                 </td>
    Line 64: <td>
    Line 65: <%=Html.DropDownList("", "Category", (SelectList)ViewData["Categories"])%>
    Line 66: </td>
    Line 67: </tr>


    Stack Trace:

    [ArgumentNullException: Value cannot be null.
    Parameter name: selectList]
    System.Web.Mvc.HtmlHelper.SelectInternal(String optionLabel, String name, MultiSelectList selectList, Boolean usedViewData, Boolean allowMultiple, IDictionary`2 htmlAttributes) +204
    System.Web.Mvc.HtmlHelper.DropDownList(String optionLabel, String name, SelectList selectList, IDictionary`2 htmlAttributes) +46
    System.Web.Mvc.HtmlHelper.DropDownList(String optionLabel, String name, SelectList selectList) +40
    ASP.views_product_addproduct_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Ting\SW7\Projekt\Development\Website\Trunk\BilNyt\BilNyt\Views\Product\AddProduct.aspx:65
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +98
    System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20
    System.Web.UI.Page.Render(HtmlTextWriter writer) +26
    System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +56
    System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2558

    Here is my source code for the InsertProduct method which is called whenever I press the "Add Product" button in the Addproduct view:

     

    1            public ActionResult InsertProduct( string name, double? price, int? quantity, string description, string technicalDescription, int category, string location, string manufacturer)
    2 {
    3 Product p = new Product();
    4 if (name == "")
    5 {
    6 TempData["message"] = "Product Name is missing";
    7 return View("AddProduct", p);
    8 }
    9
    10 if (price == null || price < 0)
    11 {
    12 TempData["message"] = "Price incorrectly filled out";
    13 return View("AddProduct", p);
    14 }
    15
    16 if (quantity == null || quantity < 0)
    17 {
    18 TempData["message"] = "Quantity incorrectly filled out";
    19 return View("AddProduct", p);
    20 }
    21 p.Name = name;
    22 p.Location = location;
    23 p.Manufacturer = manufacturer;
    24 p.Price = (double)price;
    25 p.Quantity = (int)quantity;
    26 p.CategoryID = category;
    27 p.Category = m_databaseConnection.GetCategoryById(category);
    28 p.Description = description;
    29 p.TechnicalDescription = technicalDescription;
    30
    31 m_databaseConnection.Products.InsertOnSubmit(p);
    32 m_databaseConnection.SubmitChanges();
    33 TempData["message"] = "Product Inserted!";
    34 return View("AddProduct");

      

    The AddProduct view is supported by this code in the ProductController class:

     

    1            public ActionResult AddProduct()
    2 {
    3 ViewData["Categories"] = new SelectList(m_databaseConnection.GetCategories(), "CategoryID", "Name");
    4 return View();
    5 }
      

     

     

     

    Sunday, October 5, 2008 1:04 PM

All replies

  • User-708842294 posted

    When you call return View("AddProduct") in InsertProduct action method, you also have to assign value to ViewData["Categories"]. Otherwise, the ViewData["Categories"] is null, causing an exception.

     

    Regards,

    Agus Suhanto

    Sunday, October 5, 2008 4:35 PM
  • User1652527618 posted

    Hi,

    Would it be a good idea to simply "return AddProduct()", instead of "return View("AddProduct")?

    Since Categories gets set in the method, and it would keep the code one place?

    Or is it generally a bad idea to call one ActionResult from another ActionResult?

      - Ceder

     

    Sunday, October 5, 2008 5:25 PM
  • User-708842294 posted

    Nothing prevents you to do so. The only downside is it will create tight coupling between the view for POST and view for GET. When you change the view for GET, you have to remember that the view is also called from the POST action too.

     

    Regards,

    Agus Suhanto

    Sunday, October 5, 2008 6:48 PM
  • User1994633106 posted

     

    thanks :-) I made the same discovery shortly after my post.
    Monday, October 6, 2008 4:29 PM