如何修复Ajax调用“参数字典包含空条目.........”

时间:2019-05-16 07:45:07

标签: javascript asp.net ajax asp.net-mvc

我正尝试通过ajax调用将数据发送到控制器,但是我检查了所有代码,调试了所有值,但它不断向我显示此错误。事件也向我显示了MVC通话

  

对于系统方法'System.Int32' 1 [System.Web.HttpPostedFileBase],参数字典中的参数' BrandId '具有非空类型'System.Web.Mvc.ActionResult Forms(System.Collections.Generic.IEnumerable,其中包含空条目。字符串,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();
}

我希望将数据发送到控制器

1 个答案:

答案 0 :(得分:0)

像打击一样将您的BrandId解析为int并追加到数据中

parseInt(Brand)