【发布时间】:2019-05-16 07:45:06
【问题描述】:
我正在尝试通过 ajax 调用将我的数据发送到控制器,但我检查了我调试的所有代码,所有值都已填充,但它不断向我显示此错误。事件它也向我展示了 MVC 调用以及
参数字典包含非可空类型
'System.Int32'的参数“BrandId”的空条目,用于方法'System.Web.Mvc.ActionResult Forms(System.Collections.Generic.IEnumerable1[System.Web.HttpPostedFileBase]、System.String、Int32、 Int32,布尔值,Int32)'in'AdminDevVersion.Controllers.HomeController'`。可选参数必须是引用类型、可空类型或声明为可选参数。 参数名称:参数
我已经调试了代码,我检查了是否所有的值都被填满了。
我的 Ajax 代码
function uploadSubmitHandler() {
var PName = $("#ProductName").val();
var Category = $("#CategoryDropDown").val();
var Brand = $("#BrandDropDown").val();
var SubCategory = $("#SubCategory").val();
var ProductDescription = $('textarea.textarea-editor').val();
var NewOROldRadio = $("input[name='ProductStatus']:checked").val();
if (state.fileBatch.length !== 0) {
var data = new FormData();
for (var i = 0; i < state.fileBatch.length; i++) {
data.append('files', state.fileBatch[i].file, state.fileBatch[i].fileName);
}
data.append('ProductName', PName);
data.append('CategoryId', Category);
data.append('BrandId', Brand);
data.append('IsNewStatus', NewOROldRadio);
data.append('SubCategoryId', SubCategory);
data.append('Description', ProductDescription);
$.ajax({
type: 'POST',
url: options.ajaxUrl,
data: data,
cache: false,
contentType: false,
processData: false
});
}
}
控制器代码
[HttpPost]
public ActionResult Forms(IEnumerable<HttpPostedFileBase> files, String ProductName, int CategoryId, int BrandId, bool IsNewStatus, int SubCategoryId)
{
List<string> FileNames = new List<string>();
string ImageName = null;
TblProduct InsertProduct = new TblProduct();
if (ModelState.IsValid == true)
{
InsertProduct.Name = ProductName;
InsertProduct.IsActive = IsNewStatus;
InsertProduct.BrdId = BrandId;
InsertProduct.CatId = SubCategoryId;
InsertProduct.Image = ImageName;
InsertProduct.Created = DateTime.Now;
InsertProduct.IsActive = true;
_RepoProduct.Insert(InsertProduct);
_RepoProduct.Save();
TblProRelImg RelatedImages = new TblProRelImg();
foreach (HttpPostedFileBase file in files)
{
string _path = System.IO.Path.Combine(Server.MapPath("~/Content/Images/"), file.FileName);
file.SaveAs(_path);
if (file == files.First())
{
ImageName = file.FileName.ToString();
}
else
{
RelatedImages.PrdID = InsertProduct.ID;
RelatedImages.Image = file.FileName;
_ReporRelatedImages.Insert(RelatedImages);
_ReporRelatedImages.Save();
}
FileNames.Add(file.FileName);
}
ViewBag.CategoryId = Logics.Category();
ViewBag.BrandInfo = new SelectList(DbContext.TblBrands, "Id", "Name");
}
return View();
}
我希望将数据发送到控制器
【问题讨论】:
-
你能显示服务器上的调试显示什么吗?变量中有什么?
-
html表单有值
-
$("#BrandDropDown").val()的返回类型是什么?数字还是字符串?您的控制器需要BrandId的号码。如果您想允许 Null 则将其设为可空 int -int? -
int & 它得到正确的 int
-
在 $.ajax 之前设置调试器并检查数据对象值。错误表示您的 BrandId 属性为空。它期望 int 值
标签: javascript asp.net ajax asp.net-mvc