我正在尝试更新MVC视图,以便在表单发布后显示消息,但无法使其正常工作。我是新手,所以我会欣赏一些关于我做错的几点建议。这不是表单验证,因为在表单发布和处理之后才会知道逻辑和响应消息。
控制器操作[HttpPost] public async Task<IActionResult> PlatformRecord(RecAction RecActionNotUSed)
在Form POST上触发并执行一些代码隐藏逻辑 - 然后我需要使用Response更新View,这只是一个临时消息而不是存储在任何地方。
初始GET
请求正常工作并显示消息,但在表单POST后无法弄清楚如何执行相同操作。
我尝试添加ModelState.Clear();
但没有成功。此外,如果我重定向到初始视图,我会丢失响应消息,例如return RedirectToAction("PlatformRecord");
表示我不再拥有RecAction.Response = "NEED TO SHOW THIS RESPOSNE MESSAGE AFTER FORM POST.";
我的代码如下
视图模型:
public class RecAction
{
public RecUser RecUser { get; set; }
public string Response { get; set; }
}
控制器:
public class RecordManagerController : Controller
{
private readonly IOptions<ConnectionStrings> _connectionStrings;
private readonly UserManager<AppRecationUser> _userManager;
Public RecordManagerController(UserManager <AppRecationUser> UserManager,
IOptions <connectionStrings> connectionStrings)
{
_userManager = userManager;
_connectionStrings = connectionStrings;
}
// GET: /<controller>/
public IActionResult Index()
{
return View();
}
private Task<AppRecationUser> GetCurrentUserAsync()
{
return _userManager.GetUserAsync(HttpContext.User);
}
public async Task<IActionResult> PlatformRecord()
{
var RecordDataModel = new RecordDataModel(_connectionStrings.Value.DefaultConnection);
var user = await GetCurrentUserAsync();
RecAction RecAction = new RecAction();
RecAction.RecUser = RecordDataModel.GetRecord(user.Email, "Platform");
if (RecAction.RecUser.Record == null)
{
//Response Successfully Displayed
RecAction.Response = "No Record found";
}
return View(RecAction);
}
[HttpPost]
public async Task<IActionResult> PlatformRecord(RecAction RecActionNotUSed)
{
try
{
if (ModelState.IsValid)
{
var RecordDataModel = new RecordDataModel(_connectionStrings.Value.DefaultConnection);
var user = await GetCurrentUserAsync();
RecAction RecAction = new RecAction();
RecAction.RecUser = RecordDataModel.GetRecord(user.Email, "Platform");
RecSettings latestSettings = RecordDataModel.GetSettings();
RecKeys RecKeys = RecordDataModel.GetKey();
if (RecKeys.PrivateKey == null)
{
ModelState.Clear();
//Rsponse not updating
RecAction.Response = "NEED TO SHOW THIS RESPOSNE MESSAGE AFTER FORM POST.";
return View(RecAction);
}
return RedirectToAction("PlatformRecord");
}
Else
{
//Need to return the same view for errors so the validation is not overwritten.
return View();
}
}
catch
{
// If we got this far, something failed, redisplay form
return RedirectToAction("PlatformRecord");
}
}
}
查看:
@model ProjectXYZ.Models.RecordModels.RecAction
@{
ViewData["Title"] = "PlatformRecord";
}
<h2>Platform Record</h2>
<form asp-controller="RecordManager" asp-action="PlatformRecord" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal" autocomplete="off">
<p></p>
<hr />
<div class="form-group">
<label asp-for="RecUser.Record" class="col-md-2 control-label"></label>
<div class="col-md-10">
<textarea asp-for="RecUser.Record" class="form-control" cols="1" rows="8" readonly></textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Response" class="text-danger" readonly/>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn btn-default">Request New Record</button>
</div>
</div>
</form>