我的問題在於我的代碼。我的ViewBag.Message不工作,我不知道爲什麼它沒有顯示在我的網頁上,最終它沒有將我重定向到索引並卡在我的當前控制器上。這裏是我的代碼:ViewBag.Message不能正常工作
控制器
public IActionResult AddPayments()
{
try
{
var sqlQuery = "INSERT INTO LoanPayments (LoanID, DateOfPayment, AmountOfPayment, Remarks) " +
"SELECT lc.LoanID, " +
" CONVERT(DATE, GETDATE(), 101), " +
" ISNULL(CAST(((lt.InterestRate/100) * lc.LoanAmount) + lc.LoanAmount/((dbo.fnNumberOfYears(CONVERT(VARCHAR(15), LoanDateStart, 101), CONVERT(VARCHAR(15), LoanPaymentDue, 101)) * 12) * 2) AS DECIMAL(18,2)), 0), " +
" 'Loan Ledger Created For ' + CAST(GETDATE() AS VARCHAR(50)) " +
" FROM LoanContract lc " +
" INNER JOIN LoanType lt ON lt.LoanTypeID = lc.LoanTypeID ";
_Context.Database.ExecuteSqlCommand(sqlQuery);
ViewBag.Message = "Has successfully created payments today.";
return RedirectToAction("Index");
}
catch (Exception)
{
throw;
}
}
public IActionResult Index()
{
return View();
}
查看
<h2>Create Payment</h2>
<span>@ViewBag.Message</span>
<a asp-controller="Payment" asp-action="AddPayments" class="btn btn-primary">Secondary</a>
任何幫助,將不勝感激!
http://stackoverflow.com/questions/7756464/how-do-i-render-html-from-the-viewbag-using-mvc3-razor –
嗨@BobSwager我試過了,但它也不起作用...並且它似乎沒有在html中呈現任何東西。 –
您必須從AddPayments()返回視圖。不要將其重定向到索引。 –