首先,我正在學習 Asp.net 核心。我有一個如下視圖,當我在 blogItemId 中檢索一個專案并將其作為模型發送到DetailedView.cshtml
如下:
[HttpGet]
public IActionResult DetailedView(int blogItemId)
{
var item = _mainBlogHelperModel.BlogDataItems.FirstOrDefault(x => x.Id == blogItemId);
if (item == null)
{
return RedirectToAction("MainBlog", new { page = 1, blogCategory = "" });
}
return View(item);
}
現在DetailedView.cshtml
,我有一個表格。當我單擊表單中的“發表評論”按鈕時,我想在資料庫中保存一些模型記錄并在視圖中更新一些部分。
問題 1:請參閱下面的模型類,我有一個導航屬性和外鍵。如何從我的模型中在 jQuery 中指定這些值。
問題 2:我在PostComment
方法中有一個斷點,但 jQuery 沒有系結任何值。一切都是空的。請幫忙。
這是我的DetailedView.cshtml
:
@model KGSBlog.Models.BlogData
@SOME CODE HERE @
<form id="formPostComment" method="post" action="BlogData/PostComment">
<div class="row">
<div class="col-md-6">
<input type="text" placeholder="Name *" id="name" name="name">
</div>
<div class="col-md-6">
<input type="email" placeholder="Email *" id="email" name="email">
</div>
</div>
<div class="row">
<div class="col-xs-12">
<input type="text" placeholder="WebSite" name="website">
</div>
</div>
<textarea name="message" placeholder="Message *" id="message" cols="45" rows="10"></textarea>
<button id="btnPostComment" class="btn btn-main m-bottom-40">Post Comment</button>
</form>
@section Scripts {
<!-- jQuery -->
<script src="~/js/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="~/bootstrap/js/bootstrap.min.js"></script>
<!-- Components Plugin -->
<script src="~/js/jquery.easing.1.3.js"></script>
<script type="text/javascript">
$(function () {
$("#btnPostComment").click(function () {
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
var dataToPost = {};
dataToPost.Name = name;
dataToPost.Email = email;
dataToPost.Comment = message;
//How can I populate the BlogId and Blog properties here?
$.ajax({
url: '@Url.Action("PostComment", "BlogData")',
data: JSON.stringify(dataToPost),
type: "POST",
dataType: 'JSON',
contentType: "application/json",
success: function(data) {
//do stuff with json result
},
error: function(passParams) {
console.log("Error is " passParams);
}
});
});
});
</script>
}
這是我的BlogDataController
PostComment
操作方法,其中引數中的屬性blogComment
全部為空。
[HttpPost]
public async Task<IActionResult> PostComment(BlogComments blogComment)
{
if (blogComment != null)
{
await _context.BlogComments.AddAsync(blogComment);
await _context.SaveChangesAsync();
}
var item = _mainBlogHelperModel.BlogDataItems.FirstOrDefault(x => x.Id == blogComment.Id);
return new JsonResult(new { comment = blogComment.Comment, name = blogComment.Name });
}
BlogComments
模型類
public class BlogComments
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Comment { get; set; }
[ForeignKey("BlogID")]
public int BlogId { get; set; }
public BlogData Blog { get; set; }
}
uj5u.com熱心網友回復:
問題 1:理想情況下,您不想傳遞Blog
屬性,只想傳遞BlogId
. 這不會導致您的問題,但在我看來,您不應該使用物體作為模型,您應該有另一個充當 DTO 的類。使用物體物件作為資料傳輸物件是一種好習慣嗎?
你可以用這樣的東西做你正在尋找的東西:
dataToPost.BlogId = @Model.Id;
問題 2:它可能會在您解決問題 1 后得到解決。問題可能是您的操作需要一個int
forBlogId
但您當前沒有通過BlogId
。
如果這不起作用,請在瀏覽器中打開網路選項卡并確保它正在傳遞 POST 正文。然后檢查ModelState
您的操作中的錯誤以查看是否有任何屬性未通過驗證。
uj5u.com熱心網友回復:
由于在您的原始帖子中您使用的是 contentType:“application/json”,因此您只需要添加一個 [FromBody] 屬性,一切都會正常作業
public async Task<IActionResult> PostComment([FromBody]BlogComments blogComment)
在您的第二篇文章中,代碼正在運行,因為在這種情況下,您的內容型別是表單資料(默認情況下)。在這種情況下,您不需要 [FromBody] 屬性
uj5u.com熱心網友回復:
我不知道我寫的和這個有什么區別,除了一些屬性,但是在參考這篇文章之后,我將jquery代碼更改如下,它可以作業。如果有人能告訴我為什么我的舊代碼不起作用而這個起作用,那就太好了。(可能是我不應該對物件進行字串化)
首先,我制作了這樣的表格,
<form id="formPostComment" method="post" enctype="multipart/form-data" asp-action="PostComment" asp-controller="BlogData">
<div class="row">
<div class="col-md-6">
<input type="text" placeholder="Name *" id="name" >
</div>
<div class="col-md-6">
<input type="email" placeholder="Email *" id="email">
</div>
</div>
<div class="row">
<div class="col-xs-12">
<input type="text" placeholder="WebSite" name="website">
</div>
</div>
<textarea placeholder="Message *" id="message" cols="45" rows="10"></textarea>
<submit id="btnPostComment" class="btn btn-main m-bottom-40">Post Comment</button>
</form>
@This is the jquery code.@
@section Scripts{
<!-- jQuery -->
<script src="~/js/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="~/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btnPostComment").click(function () {
var url = $("#formPostComment").attr("action");
var formData = new FormData();
formData.append("Name", $("#name").val());
formData.append("Email", $("#email").val());
formData.append("Comment", $("#message").val());
formData.append("BlogId",@Model.Id);
$.ajax({
type: 'POST',
url: url,
data: formData,
processData: false,
contentType: false
}).done(function (response) {
if (response.Status === "Success") {
alert("HELLO");
}
});
});
});
</script>
控制器代碼,我只是像這樣更改了引數。即使使用舊的 jquery 代碼,我也嘗試過這樣的 Bind 并且它不起作用。
public async Task<IActionResult> PostComment([Bind("Name,Email,Comment,BlogId")] BlogComments blogComment)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/513434.html
下一篇:Java反序列化之C3P0鏈學習