1

對於一般開發和MVC,我很新。 我想顯示我的存儲庫在我的MVC網站中引發的異常。我創建了View:「Error」來處理錯誤消息。 我的主控制器如下所示:在MVC中顯示捕獲異常

using BankMVC.Models; 
using BankMVC.WithdrawingService; 
using Pocos; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace BankMVC.Controllers 
{ 
    public class WithdrawingController : Controller 
    { 
     WithdrawingServiceClient withdrawingClient; 

     public WithdrawingController() 
     { 
      withdrawingClient = new WithdrawingServiceClient(); 
     } 

     public ActionResult Withdraw(int Id, int TypeId) 
     { 
      BankAccount bankAccount = new BankAccount(); 
      bankAccount.BankAccountId = Id; 
      bankAccount.BankAccountTypeId = TypeId; 
      DepositingWithdrawingViewModel withdrawViewModel = 
       new DepositingWithdrawingViewModel(); 
      withdrawViewModel.BankAccountId = bankAccount.BankAccountId; 
      withdrawViewModel.Balance = bankAccount.Balance; 
      withdrawViewModel.BankAccountTypeId = bankAccount.BankAccountTypeId; 
      return View(withdrawViewModel); 
     } 

     [HttpPost] 
     public ActionResult Withdraw(DepositingWithdrawingViewModel withdrawViewModel) 
     { 
      BankAccount bankAccount = new BankAccount(); 
      bankAccount.BankAccountId = withdrawViewModel.BankAccountId; 
      bankAccount.BankAccountTypeId = withdrawViewModel.BankAccountTypeId; 

      try 
      { 
       withdrawingClient.Withdraw(withdrawViewModel.Amount, bankAccount); 
       return RedirectToAction("Index", "BankAccount"); 
      } 

      catch (Exception e) 
      { 
       return RedirectToAction("Error", new { message = e.Message }); 
      } 

     } 

    } 
} 

我的主控制器圖如下所示:

@model BankMVC.Models.DepositingWithdrawingViewModel 

@{ 
    ViewBag.Title = "Withdraw"; 
} 

<h2>Withdraw</h2> 


@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>Withdraw from Bank Account</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.BankAccountId, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.BankAccountId, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.BankAccountId, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.BankAccountTypeId, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.BankAccountTypeId, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.BankAccountTypeId, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Balance, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Balance, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Balance, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Amount, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Amount, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Amount, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Withdraw" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index", "BankAccount") 
</div> 

@section Scripts { 
     @Scripts.Render("~/bundles/jqueryval") 

} 

我已經創建了一個錯誤模型,如下所示:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace BankMVC.Models 
{ 
    public class ErrorModel 
    { 
     public string Error { get; set; } 
    } 
} 

應該如何我的錯誤 - 查看外觀就像顯示錯誤消息。目前看起來如下:

@model BankMVC.Models.ErrorModel 

@{ 
    ViewBag.Title = "Error"; 
} 

<h2>Error</h2> 

任何澄清表示讚賞。

回答

0

我看到您正在重定向到名爲「Error」的動作,以顯示錯誤頁面。但是我沒看到的是一個名爲「Error」的操作方法。請執行下列操作

中創建一個名爲「錯誤」

public ActionResult Error(string message) 
{ 
    var model = new ErrorModel(); 
    model.Error = message; 
    return View(model); // make sure your error view is named as "Error". Else u need to specify the view name in the return command. 
} 

請注意,我使用的是u必須張貼您的誤差模型控制器的另一種方法。

在錯誤查看

此外,您還可以通過改變這樣的

視圖利用該錯誤信息這將顯示您的錯誤信息,以及