【发布时间】:2014-03-11 16:56:50
【问题描述】:
上传文件时出现以下错误。
参数字典包含一个 参数“category_id”的空条目 不可为空的类型'System.Int32' 对于方法 'System.Web.Mvc.ActionResult AddProduct(Int32, System.String, 单,System.String,System.String, 系统字符串,系统字符串, System.String, System.String)' 在 '电子商务.Controllers.AdminController'。 使参数可选其类型 应该是引用类型或 可空类型。参数名称: 参数
我正在使用一个对话框。
观点
<script type="text/javascript">
$(function() {
$("#dialog").dialog({
bgiframe: true,
height: 140,
modal: true,
autoOpen: false,
resizable: false
})
});
</script>
<div id="dialog" title="Upload files">
<% using (Html.BeginForm("Upload", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<p><input type="file" id="fileUpload" name="fileUpload" size="23"/> </p>
<p><input type="submit" value="Upload file" /></p>
<% } %>
</div>
<p>
<label for="image_name">image_name:</label>
<a href="#" onclick="jQuery('#dialog').dialog('open'); return false">Upload File</a>
<%= Html.ValidationMessage("image_name", "*") %>
</p>
控制器动作
public ActionResult AddProduct(int category_id, string product_name, float product_price, string product_desc, string weight, string image_name, string img_content, string available_qty, string isFeature)
{
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase file = Request.Files[inputTagName];
if (file.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(file.FileName));
string filecontent = Path.Combine(HttpContext.Server.MapPath("../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 = "Featured";
}
else
{
isFeature = "NotFeatured";
}
int i = _adminImplementation.addproduct(category_id, product_name, product_price, product_desc, weight,image_name ,img_content ,available_qty ,isFeature );
ViewData["succm"] = "Product added successfully";
return View();
}
请提出一些有用的答案。
谢谢
丽兹
【问题讨论】:
-
我们还需要方法签名。
-
现在我已经解决了您的问题,请提供最初呈现页面的 ViewResult。我需要知道缺失值在哪里。此外,如果您不知道为什么会出现此错误,我建议您访问 www.asp.net/mvc/,因为您似乎不了解 ASP.NET 中 MVC 架构的一些基本元素。
标签: asp.net-mvc upload