【发布时间】:2020-08-10 13:35:47
【问题描述】:
我在 asp.net 核心应用程序中创建了一个弹出窗口,以使用 Ajax、jQuery 和引导程序创建和编辑新书,但是当我在此表单中插入数据并发布时,出现此错误 500 Internal Server Error : enter image description here
这是我在控制器中的功能
[HttpPost]
public IActionResult AddOrEdite(int id, BookViewModel bookViewModel)
{
if (!ModelState.IsValid)
{
if (id == 0)
{
string fileName = string.Empty;
if (bookViewModel.File != null)
{
string uploads = Path.Combine(hosting.WebRootPath, "uploads");
fileName = bookViewModel.File.FileName;
string fullPath = Path.Combine(uploads, fileName);
bookViewModel.File.CopyTo(new FileStream(fullPath, FileMode.Create));
bookViewModel.Book.imageURL = fileName;
}
if (!ModelState.IsValid)
{
bookViewModel.Authors = _authorRepository.GetAll();
return View(bookViewModel);
}
Book book = new Book
{
Bookid = bookViewModel.Book.Bookid,
Title = bookViewModel.Book.Title,
price = bookViewModel.Book.price,
Authorid = bookViewModel.Book.Authorid,
Borrowerid = bookViewModel.Book.Borrowerid,
Author = bookViewModel.Book.Author,
Borrower = bookViewModel.Book.Borrower,
imageURL = fileName
};
_bookRepository.Create(bookViewModel.Book);
}
else
{
bookViewModel.Authors = _authorRepository.GetAll();
_bookRepository.Update(bookViewModel.Book);
}
return Json(new { isValid = true, html = GlobalClass.RenderRazorViewToString(this, "_ViewAllBook", _context.Books.ToList()) });
}
return Json(new { isValid = false, html = GlobalClass.RenderRazorViewToString(this, "AddOrEdit", bookViewModel) });
}
这是我在 site.js 中的函数
jQueryAjaxPost = form => {
try {
$.ajax({
type: 'POST',
url: form.action,
data: new FormData(form),
contentType: false,
processData: false,
success: function (res) {
if (res.isValid) {
$('#view-all').html(res.html)
$('#form-modal .modal-body').html('');
$('#form-modal .modal-title').html('');
$('#form-modal').modal('hide');
}
else
$('#form-modal .modal-body').html(res.html);
},
error: function (err) {
console.log(err)
}
})
return false;
} catch (x) {
console.log(x)
}}
@model BookViewModel
@{
Layout = null;
}
<div class="row orderForm">
<form asp-controller="Book" asp-action="AddOrEdite" onsubmit="return jQueryAjaxPost(this);">
<input type="hidden" asp-for="@Model.Book.Bookid" />
<div class="form-group">
<label asp-for="Book.Author" class="col-md-2 control-label"></label>
<div class="col-lg-12">
@Html.DropDownListFor(m => m.Book.Authorid,
new SelectList(Model.Authors.Select(x => new { Value = x.AuthorId, Text = x.Name }), "Value", "Text"), "Select an author",
htmlAttributes: new { @class = "form-control border-input" })
<span asp-validation-for="Book.Author" class="text-danger"></span>
</div>
<div class="col-lg-12 center">
<a class="btn btn-primary center" style="width:100%" asp-controller="Author" asp-action="Create">Create an author</a>
</div>
</div>
<div class="row col-lg-12">
<div class="col-md-6">
<div class="form-group">
<label asp-for="Book.Title" class="col-lg-2 control-label"></label>
<div>
<input asp-for="Book.Title" class="form-control" placeholder="Enter book title" />
<span asp-validation-for="Book.Title" class="text-danger"></span>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label asp-for="Book.price" class="col-lg-2 control-label"></label>
<div>
<input asp-for="Book.price" class="form-control" placeholder="Enter book title" />
<span asp-validation-for="Book.price" class="text-danger"></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<label asp-for="File" class="col-lg-2 control-label"></label>
<div class="col-lg-12">
<input type="file" asp-for="File" class="form-control" placeholder="Enter Image of the book" />
<span asp-validation-for="File" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="btn-group col-md-12">
<input class="btn btn-success center-block" type="submit" value="Create" />
</div>
</div>
</form>
</div>
这是我的模特
public class Book
{
[Key]
public int Bookid { get; set; }
[Required,MinLength(2),MaxLength(200)]
public string Title { get; set; }
public int price { get; set; }
public int Quantity { get; set; }
public double Amount { get; set; }
public string imageURL { get; set; }
[ForeignKey("AuthorId")]
public int Authorid { get; set; }
public virtual Author Author { get; set; }
[ForeignKey("CustomerId")]
public int ? Borrowerid { get; set; }
public virtual Customer Borrower { get; set; }
}
这是我的视图模型
public class BookViewModel
{
public Book Book { get; set; }
public IEnumerable<Author> Authors { get; set; }
[NotMapped]
public IFormFile File { get; set; }
}
谁能帮帮我:(?
【问题讨论】:
-
您好,请发布您的
form代码和错误信息的详细信息。 500 表示服务器错误。您可以专注于控制器并逐步调试。 -
@Lightman这就是
<form>的所有代码?您可以更新问题。@Lightman 我更新了我的问题嗨,@Mostafa Gamal,如果您有关于此问题的最新信息,请告诉我们。
标签: jquery ajax asp.net-mvc asp.net-core asp.net-ajax