[英]Passing message in ViewBag from controller to view in ASP.Net MVC
[英]ASP.NET: Pass message from controller to view (ViewBag, ViewData or TempData)
在我的ASP.NET项目中,我具有一个允许用户更改其密码的视图。 提交表单后,将使用RedirectToAction
将用户重定向到UserDetail。 在这里,我想通知用户密码更改是否成功(计划使用Alertify JS )。 但是,我似乎无法成功地将消息从控制器传递给视图。
当使用RedirectToAction
而不使用ViewBag
, ViewData
, Tempdata
,...时,如何正确地将数据(简单字符串)从控制器传递到视图? 或者,如果有办法解决我目前遇到的这些困难,不胜感激。
ViewBag
/ ViewData
:无法使用,因为我返回了RedirectToAction
,我无法在URL而不是View
传递它。 我已经考虑过使用Ajax
返回JSON
而不是使用RedirectToAction
,这将使我能够使用ViewBag
但这会带来一些困难,并会改变项目的整体结构。
TempData
:我没有在Startup.cs
配置session
,也不能这样做,因为我缺少Microsoft.AspNetCore.Session
命名空间。 我也无法添加名称空间,因为这会增加一些复杂性。 请参见以下错误: System.TypeLoadException: 'Method 'ConfigureAppConfiguration' in type 'Microsoft.AspNetCore.Hosting.WebHostBuilder' from assembly 'Microsoft.AspNetCore.Hosting, Version=1.0.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.'
我宁愿不使用Cookie或某种现金系统。
编辑:我也宁愿不使用查询字符串,但看到注释/答案使我意识到我的选择是有限的。 我忘记了将querystring / route参数添加到较早的列表中。
[HttpGet]
public IActionResult UserDetail(int id)
{
// Code to GET the UserViewModel
return View(model);
}
[HttpGet]
public IActionResult UserPassword(int id)
{
// Code to GET the UserViewModel
return View(model);
}
[HttpPost]
public IActionResult UserPassword(UserViewModel model)
{
// Code to check and save the password.
if (user.Password == model.Password) {
//ViewData["Message"] = "Succeeded";
//TempData["Message"] = "Succeeded";
} else {
//ViewData["Message"] = "Failed";
//TempData["Message"] = "Failed";
}
// TODO: Alert the user
return RedirectToAction("UserDetail", new { id = model.UserId });
}
<!-- Some view code -->
<form>
<button class="ui button button-back" type="button" onclick="window.location.href = '@Url.Action("UserPassword", "Manage", new { id = Model.UserId })';">Change Password</button>
</form>
<!-- Trying to use ViewData -->
@*<div id="id-message" data-value="@ViewData["Message"]"></div>*@
<script>
$(document).ready(function () {
console.log("Ready");
// Alert the user of a password change
//var message = $("#id-message").attr("data-value");
//var message = "@(TempData["Message"] as string)";
if (message !== null && message !== "") {
alertify
.alertmessage, function () {
alertify.message('OK');
};
}
});
// Some script code
</script>
您可以使用RedirectToAction()
方法将其作为路由参数传递
return RedirectToAction("UserDetail", new { id = model.UserId, message= "" });
并接受
[HttpGet]
public IActionResult UserDetail(int id, string message)
{
// Code to GET the UserViewModel
return View(model);
}
关于您所描述的另一个选项,我建议使用SessionState:
string passwordChange = "Password has been changed";
Session["yourName"] = passwordChange // inside your if else logic calling it from your UserDetail controller method.
或创建一个JsonResult结果来引用一个Ajax调用。
更改:
[HttpGet]
public IActionResult UserDetail(int id)
至
[HttpPost]
public IActionResult UserDetail(UserViewModel model)
将Message属性添加到模型中。
应该为你做
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.