这个空条目错误是什么意思?

时间:2009-11-19 10:49:12

标签: asp.net

我在MVC应用程序中收到以下错误。

  

参数字典包含非可空类型'System.Int32'的参数'isFeature'的空条目,用于方法'System.Web.Mvc.ActionResult AddProduct(System.String,System.String,System.String,System) “ExportJeterygridtoexcel.Controllers.AdminController”中的.String,System.String,System.String,System.String,System.String,Int32)'。要使参数可选,其类型应为引用类型或Nullable类型。   参数名称:参数

我在数据库中插入一个产品,我的一个字段是isFeature,它在数据库中是int类型

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 ();
}

实现类代码如下:

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;
}

视图页面如下:

<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>

有没有人知道我做错了什么?

3 个答案:

答案 0 :(得分:0)

尝试使用Int32?作为参数类型。

整数不可为空。 添加'?'你说它是可空的。

答案 1 :(得分:0)

确保您正在调用的函数获取所有填充的参数(非空)。

您需要提供错误代码示例以防万一。

答案 2 :(得分:0)

isFeature来自Checkbox。如果未选中,则不会发布任何内容。使参数可为空,如果数据库字段设置为NOT NULL,则在分配之前检查该值:

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