javascript - 如何修复 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.IEnumerable
in
我已经调试了代码,并检查了是否所有值都已填充。
我的 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();
}
我希望将数据发送到控制器
解决方案
将您的 BrandId 解析为 int 像打击并附加到您的数据
parseInt(Brand)
推荐阅读
- ruby-on-rails - 渲染:带有 URL 参数的新
- reactjs - 导航出同一主机上的 react-router 单页应用程序
- html - 外部样式表无效,但已链接
- python - Python:一步复制熊猫并替换特定列值
- firebase - 为什么部署firebase项目会给我一个路径错误?
- java - 为什么我的排序算法返回相同数量的比较?
- c++ - 我应该使用 Nuget 或 Vcpkg 安装适用于 C++ 的 AWS 开发工具包吗?
- javascript - 我怎样才能让我的倒计时计时器在我每次用 JavaScript 刷新时停止重新开始
- r - R list.files 未显示所有文件(可能是路径问题?)
- reactjs - 访问 reactjs 结构中的元素