[英]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.