2009-11-19 4 views
-1

Je reçois l'erreur suivante dans mon application MVC.Que signifie cette erreur d'entrée nulle?

Les paramètres dictionnaire contient une entrée nulle pour le paramètre 'isFeature' de type non-nullable 'System.Int32' pour la méthode « System.Web.Mvc.ActionResult addProduct (System.String, System.String, système. String, System.String, System.String, System.String, System.String, System.String, Int32) 'dans' ExportJquerygridtoexcel.Controllers.AdminController '. Pour rendre un paramètre facultatif, son type doit être un type de référence ou un type Nullable. Nom du paramètre: Paramètres

Je suis l'insertion d'un produit dans la base de données et l'un de mes champ est isFeature qui est de type int dans la base

public ActionResult AddProduct(string Categories, 
           string product_name, 
           string product_price, 
           string product_desc, 
           string weight, 
           string image_name, 
           string img_content, 
           string available_qty, 
           int isFeature) 
{ 
    string s1, type; 

    foreach (string inputTagName in Request.Files) 
    { 
     HttpPostedFileBase file = Request.Files[inputTagName]; 
     if (file.ContentLength > 0) 
     { 
      image_name = Path.GetFileName(file.FileName); 
      img_content = Path.GetFileName(file.ContentType); 

      file.SaveAs(HttpContext.Server.MapPath("../Content/upload_ProdImg/") + image_name); 
      System.Drawing.Size r = new Size(80, 80); 
      System.Drawing.Image srcImage = System.Drawing.Image.FromFile(Server.MapPath("../Content/upload_ProdImg/") + image_name); 
      System.Drawing.Image tnImage = srcImage.GetThumbnailImage(r.Width, r.Height, null, IntPtr.Zero); 
      System.Drawing.Graphics graphic = Graphics.FromImage(tnImage); 
      System.Drawing.Rectangle rect = new Rectangle(0, 0, r.Width, r.Height); 
      graphic.DrawImage(tnImage, rect); 
      s1 = HttpContext.Server.MapPath("../Content/thumb_ProdImg/") + image_name; 
      tnImage.Save(s1); 
      type = img_content; 

      //string filePath = Path.Combine(HttpContext.Server.MapPath("/Content/thumb_ProdImg/"), Path.GetFileName(file.FileName)); 
      // string filecontent = Path.Combine(HttpContext.Server.MapPath("/Content/Uploads"), Path.GetFileName(file.ContentType)); 

      //image_name = Path.GetFileName(file.FileName); 
      //img_content = Path.GetFileName(file.ContentType); 

      // file.SaveAs(filePath); 
     } 
    } 

    AdminImplementation _adminImplementation = new AdminImplementation(); 

    Boolean isfeature = Convert.ToBoolean(isFeature); 

    if (isfeature) 
    { 
     isFeature = 0; 
    } 
    else 
    { 
     isFeature = 1; 
    } 

    int i = _adminImplementation.addproduct(Categories, 
              product_name, 
              product_price, 
              product_desc, 
              weight, 
              image_name, 
              img_content, 
              available_qty, 
              isFeature); 

    ViewData["succm"] = "Product added successfully"; 
    return View(); 
} 

Le code de la classe de mise en œuvre est la suivante:

public int addproduct(string categories, 
         string prodName, 
         string price, 
         string prodDesc, 
         string weight, 
         string image_name, 
         string img_content, 
         string qty, 
         int isFeature) 
{ 
    string s1 = string.Empty; 
    string s = string.Empty; 
    int itemInserted = 0; 

    using (EcommerceMVCEntities modelObject = new EcommerceMVCEntities()) 
    { 
     tbl_PRODUCTS NewItemToInsert = new tbl_PRODUCTS(); 
     //NewItemToInsert.tbl_PRODUCT_CATEGORYReference. =Convert.ToInt32(categories); 
     NewItemToInsert.product_name = prodName; 
     NewItemToInsert.product_price = Convert.ToDouble (price); 
     NewItemToInsert.product_desc = prodDesc; 
     NewItemToInsert.weight = weight; 
     NewItemToInsert.image_name = image_name; 
     NewItemToInsert.img_content = img_content; 
     NewItemToInsert.available_qty = qty; 
     NewItemToInsert.isFeature = isFeature; 

     modelObject.AddTotbl_PRODUCTS(NewItemToInsert); 
     itemInserted = modelObject.SaveChanges(); 
     modelObject.AcceptAllChanges(); 
    } 
    return itemInserted; 
} 

La page de vue est la suivante:

<table class="border"> 
    <%--<tr> 
     <td align ="right" > 
      <label for="category_id">Category:</label> 
     </td> 
     <td align ="left"> 
      <%= Html.DropDownList("Categories", (IEnumerable<SelectListItem>)ViewData["Categories"])%> 

     </td> 
    </tr>--%> 
    <tr> 
     <td align ="right" > 
      <label for="product_name">Product Name:<font color="red">*</font></label> 
     </td> 
     <td align ="left"> 
      <%= Html.TextBox("product_name") %> 
      <%= Html.ValidationMessage("product_name", "*") %> 
     </td> 
    </tr> 
    <tr> 
     <td align ="right" > 
      <label for="product_desc">Description:<font color="red">*</font></label> 
     </td> 
     <td align ="left"> 
      <%= Html.TextBox("product_desc") %> 
      <%= Html.ValidationMessage("product_desc", "*") %> 
     </td> 
    </tr> 
    <tr> 
     <td align ="right" > 
      <label for="product_price">Price:<font color="red">*</font></label> 
     </td> 
     <td align ="left"> 
      <%= Html.TextBox("product_price") %> 
      <%= Html.ValidationMessage("product_price", "*") %> 
     </td> 
    </tr> 

    <tr> 
     <td align ="right" > 
      <label for="weight">Weight:<font color="red">*</font></label> 
     </td> 
     <td align ="left"> 
      <%= Html.TextBox("weight") %> 
      <%= Html.ValidationMessage("weight", "*") %> 
     </td> 
    </tr> 
    <tr> 
     <td align ="right" > 
      <label for="image_name">Image:<font color="red">*</font></label> 
     </td> 
     <td align ="left"> 
      <input type ="file" name ="upload" id ="imgfile" runat ="server" /> 
      <%= Html.ValidationMessage("image_name", "*") %> 
     </td> 
    </tr> 
    <tr> 
     <td align ="right" > 
      <label for="available_qty">Quantity:<font color="red">*</font></label> 
     </td> 
     <td align ="left"> 
      <%= Html.TextBox("available_qty") %> 
      <%= Html.ValidationMessage("available_qty", "*") %> 
     </td> 
    </tr> 
    <tr> 
     <td align ="right" > 
      <label for="isFeature">IsFeature:<font color="red">*</font></label> 
     </td> 
     <td align ="left"> 
      <%= Html.CheckBox("isFeature") %> 
      <%= Html.ValidationMessage("isFeature", "*") %> 
     </td> 
    </tr> 
    <tr> 
     <td>&nbsp;</td> 
     <td align ="left"> 
      <input type="submit" value="Add Product" /> 
      <input type ="reset" value ="Clear" /> 
     </td> 
    </tr> 
</table> 

Est-ce que quelqu'un a une idée de ce que je fais mal?

+1

S'il vous plaît, ajoutez au moins la balise java, et modifier le titre à quelque chose qui signifie quelque chose;) –

+0

pourrait vous publiez un échantillon de code s'il vous plaît? – Justin

+0

pourquoi ajouter le tag 'java'? – akf

Répondre

0

Essayez le type de paramètre Int32?.

Un nombre entier n'est pas Nullable. En ajoutant le '?' vous dites qu'il est nul.

0

Assurez-vous que la fonction que vous appelez obtient tous les paramètres remplis (pas null).

Vous devez fournir un exemple de code bogué au cas où.

0

isFeature provient d'une case à cocher. Si ce n'est pas coché, rien n'est posté. Faire le paramètre annulable et vérifier la valeur avant de l'affecter si votre champ de base de données est définie sur NOT NULL:

NewItemToInsert.isFeature = (isFeature == null? 0:1);