Asp.net 这个空输入错误是什么意思?

Asp.net 这个空输入错误是什么意思?,asp.net,Asp.net,我的MVC应用程序中出现以下错误 参数字典包含方法“System.Web.Mvc.ActionResult AddProduct”(System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、System.String、Int32)的非空类型“System.Int32”的参数“i

我的MVC应用程序中出现以下错误

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

我正在数据库中插入一个产品,其中一个字段是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>
查看页面如下所示:

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>

产品名称:*
说明:*
价格:*
重量:*
图片:*
数量:*
IsFeature:*

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

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

整数不能为空。
通过添加“?”表示它是可空的。

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


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

isFeature来自一个复选框。如果不检查,则不会发布任何内容。如果数据库字段设置为NOTNULL,则将参数设置为NULL,并在分配参数前检查该值:

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

请至少添加java标记,并编辑表示somethin;)的标题你能发布一个代码示例吗?@Ritz,你也可以在其他帖子中找到一些关于这个网站的有用信息。勾选这个: