Asp.net core 如何在一种方法中使用两个模型?
ModelState无效。我想我传递的模型错误。知道吗 控制器:Asp.net core 如何在一种方法中使用两个模型?,asp.net-core,model-view-controller,asp.net-core-mvc,Asp.net Core,Model View Controller,Asp.net Core Mvc,ModelState无效。我想我传递的模型错误。知道吗 控制器: [Authorize] [HttpPost] public async Task<IActionResult> SendComment([Bind("CommentID,Comment,Date,AdminId")]AdminReport adminReport,int ReportID) { var x = _userReport.UserReports.Find(Re
[Authorize]
[HttpPost]
public async Task<IActionResult> SendComment([Bind("CommentID,Comment,Date,AdminId")]AdminReport adminReport,int ReportID)
{
var x = _userReport.UserReports.Find(ReportID);
x.IsViewed = true;
adminReport.UserId = x.UserId;
adminReport.AdminId = _userManager.GetUserId(HttpContext.User);
if (ModelState.IsValid){
_adminReport.Add(adminReport);
await _adminReport.SaveChangesAsync();
return View(); }
return RedirectToAction("SendDoneAdmin");
}
您将此
[required]
属性放在注释上
:
[Required]
public string Comment { get; set; }
您必须在表单中包含该输入,才能通过验证。
您可以这样添加该字段:
<div class="card-footer">
<form asp-controller="Admin" asp-action="Viewed" method="post">
<input type="hidden" value="@report.ReportID" name="ReportID" />
<input type="text" name="Comment" />
<button type="submit" class="btn btn-primary">SendComment</button>
</form>
发送评论
您将此[required]
属性放在注释上
:
[Required]
public string Comment { get; set; }
您必须在表单中包含该输入,才能通过验证。
您可以这样添加该字段:
<div class="card-footer">
<form asp-controller="Admin" asp-action="Viewed" method="post">
<input type="hidden" value="@report.ReportID" name="ReportID" />
<input type="text" name="Comment" />
<button type="submit" class="btn btn-primary">SendComment</button>
</form>
发送评论
您的报价不清楚,但必须知道通过表单传递数据
必须在表单中输入所有内容标记
控制器
public async Task<IActionResult> SendComment()
{
// write your code....
return View(new AdminReport()); // must return new object
}
您的报价不清楚,但必须知道通过表单传递数据 必须在表单中输入所有内容标记 控制器
public async Task<IActionResult> SendComment()
{
// write your code....
return View(new AdminReport()); // must return new object
}
实际上,我的“SendComment”方法有另一个视图。但它没有显示,因为ModelState无效。实际上,我的“SendComment”方法有另一个视图。但它没有显示,因为ModelState无效。
// remove all key
foreach (var key in ModelState.Keys).ToList())
ModelState.Remove(key);
// or for one
ModelState.Remove("comment ");