ASP.MVC 5 - A Way to auth message (Frontend to Backend)

/** * View - CheckBookRecord.cshtml **/ @if (null != ViewBag.ExecuteResult) { oa.viewmodel.ExecuteResultModel result = ViewBag.ExecuteResult; string message = string.Join("\\n", result.message); if (result.isSuccess) { <text> <script type="text/javascript"> $(document).ready(function () { alert('@message'); }); </script> </text> } else { <script> $(document).ready(function () { alert('@message'); }); </script> } } /** * Controller - BookController **/ BookService bookService = new BookService() ; public ActionResult CheckBookRecord() { return View(); } [HttPost] [ValidateAntiForgeryToken] public ActionResult CheckBookRecord(BookViewModel viewModel) { if(!Model.State.IsValid) { return View("CheckBookRecord"); } ExecuteResultModel result = bookService.fetchBookRecord(viewModel); ViewBag.ExecuteResult = result; return View(); } /** * Service - BookService **/ public ExecuteResultModel fetchBookRecord(BookViewModel book) { ExecuteResultModel result = new ExecuteResultModel(); if(book == null) { result.isSucess = false; result.Add(" Query is null! ") return result; } else { try { if(demical.IsNullOrWhiteSpace(book.book_id)) { result.isSuccess = false; result.message.Add("book_id is Required!"); return result; } if (string.IsNullOrWhiteSpace(book.book_name)) { result.isSuccess = false; result.message.Add("book_name is Required!"); return result; } IEnumerable<BookViewModel> borrowList = bookRepo.fetchBookRecord(book); if(borrowList.Count() > 0) { result.isSuccess = false; result.message.Add("You have borrowed this book!"); return result; } } catch(Exception ex){ result.isSuccess = false; result.message.Add(ex.ToString()); return result; } } return result; } /** * MessageViewModel **/ namespace oa.viewmodel { [Serializable] public class ExecuteResultModel { public bool isSuccess { get; set; } public List<string> message = new List<string>(); } } /** * BookViewModel **/ namespace oa.viewmodel { [Serializable] public class BookViewModel { public demical book_id { get; set; } public string book_name { get; set; } public int book_quantity { get; set; } public string book_borrow_name { get; set; } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.