首页 > 解决方案 > 如何修复 Ajax 调用“参数字典包含一个空条目......”

问题描述

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

参数字典包含方法1[System.Web.HttpPostedFileBase]、System.String、Int32、Int32、Boolean、Int32)' ' AdminDevVersion.Controllers.HomeController ''的不可为空类型的参数“BrandId”的空条目。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数'System.Int32''System.Web.Mvc.ActionResult Forms(System.Collections.Generic.IEnumerablein

我已经调试了代码,并检查了是否所有值都已填充。

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

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

标签: javascriptasp.netajaxasp.net-mvc

解决方案


将您的 BrandId 解析为 int 像打击并附加到您的数据

parseInt(Brand)

推荐阅读