1

I am trying to show the error message on view. Can you please suggest me the right approach to do this. I was thinking to add the data annotation dynamically but can't find any solution.

Thanks

[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);
}

1 Answer 1

5

I think you're looking for:

[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); 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.