1

我试图在视图中显示错误消息。您能否建议我正确的方法来做到这一点。我正在考虑动态添加数据注释,但找不到任何解决方案。

谢谢

[HttpPost]
public ActionResult AddBook(BooksModel booksModel)
{           
    try
    {
        booksModel.Account = _bookRepository.BookDetails(booksModel.Title,booksModel.RefCode, booksModel.DontHaveRefCode);
    }
    catch (FaultException faultException)
    {
        switch (faultException.Code.Name)
        {
            case "exc1":
            // Show Error Message Here
            return View("AddBook", booksModel);
            default:
            break;
        }
    }
    return View("ConfirmBook", booksModel);
}
4

1 回答 1

5

我想你正在寻找:

[HttpPost] 
public ActionResult AddBook(BooksModel booksModel) 
{            
    try 
    { 
        booksModel.Account = _bookRepository.BookDetails(booksModel.Title,booksModel.RefCode, booksModel.DontHaveRefCode); 
    } 
    catch (FaultException faultException) 
    { 
        switch (faultException.Code.Name) 
        { 
            case "exc1": 

            ModelState.AddModelError("", faultException);
            return View("AddBook", booksModel); 
            default: 
            break; 
        } 
    } 
   return View("ConfirmBook", booksModel); 
}
于 2012-07-05T16:21:29.260 回答