Mvc Return View Error Message
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow how to display a error message in view from controller,mvc 4 the company Business Learn more about hiring developers or posting ads with us Stack modelstate.addmodelerror in mvc4 Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community display success message in mvc 4 of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up MVC 4-Return error message from Controller -Show in View up vote 15 down vote favorite 4 I how to display alert message in controller mvc doing a C# project using Razor in VS2010.(MVC 4). I need to Return error message from Controller to View and show it to User. What i was tried is: CONTROLLER: [HttpPost] public ActionResult form_edit(FormModels model) { model.error_msg = model.update_content(model); ModelState.AddModelError("error", "adfdghdghgdhgdhdgda"); ViewBag.error = TempData["error"]; return RedirectToAction("Form_edit", "Form"); } VIEW: @model mvc_cs.Models.FormModels @using ctrlr = mvc_cs.Controllers.FormController @using (Html.BeginForm("form_edit", "Form", FormMethod.Post)) {
@Html.ValidationSummary("error") @Html.ValidationMessage("error") |
@Html.DisplayNameFor(model => model.content_name) Mvc Model Error Message@Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--") |
---|
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies
Show Message In Mvc 4
of this site About Us Learn more about Stack Overflow the company mvc error message attribute Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges mvc return validation error from controller Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: http://stackoverflow.com/questions/20141102/mvc-4-return-error-message-from-controller-show-in-view Sign up Controller to Return an Error Message Instead of a View? up vote 7 down vote favorite 2 I'm fairly new to ASP.NET MVC and am not sure how best to handle the following situation. A method in my controller needs to load some data based on an ID argument. Under normal circumstances, this ID argument will be set to a http://stackoverflow.com/questions/8411495/controller-to-return-an-error-message-instead-of-a-view valid ID of an entity within my database. I construct some data and place it in ViewBag, which the view uses to render the page. However, I would like some basic error handling just in case the ID argument is not valid. Although I could write a bunch of error handling code in the view, it would be much simpler not to display the view if there is a major misuse or malfunction of the site. Is there a way the controller could simply return a "Item not found" string or something like that, and display that rather than the normal view? Or perhaps someone can suggest a better idea? asp.net-mvc share|improve this question asked Dec 7 '11 at 6:54 Jonathan Wood 36.2k40157244 add a comment| 4 Answers 4 active oldest votes up vote 20 down vote accepted if (itemId == null) { return Content("Item not found"); } Or if you want to return an HTTP 404 instead: throw new HttpException(404, "Item Not Found"); share|improve this answer answered Dec 7 '11 at 6:57 Chris Fulstow 26k45986 6 I have forgotten this a few times
Tips/Tricks Top Articles Beginner Articles Technical Blogs Posting/Update Guidelines Article Help Forum Article Competition Submit an article or tip Post your Blog quick answersQ&A Ask a http://www.codeproject.com/Questions/679815/How-to-Display-a-error-message-in-view-from-contro Question View Unanswered Questions View All Questions... Linux questions C# questions ASP.NET questions SQL questions fabric questions discussionsforums All Message Boards... Application Lifecycle> Running a Business Sales / Marketing Collaboration / Beta Testing Work Issues Design and Architecture ASP.NET JavaScript C / C++ / MFC> ATL / WTL / STL Managed C++/CLI C# Free Tools Objective-C and Swift Database Hardware error message & Devices> System Admin Hosting and Servers Java .NET Framework Android iOS Mobile SharePoint Silverlight / WPF Visual Basic Web Development Site Bugs / Suggestions Spam and Abuse Watch features Competitions News The Insider Newsletter The Daily Build Newsletter Newsletter archive Surveys Product Showcase Research Library CodeProject Stuff communitylounge Who's Who Most Valuable Professionals The Lounge The Insider News how to display The Weird & The Wonderful The Soapbox Press Releases Non-English Language > General Indian Topics General Chinese Topics help What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum Site Map Advertise with us About our Advertising Employment Opportunities About Us Ask a Question All Questions All Unanswered FAQ How to Display a error message in view from controller,MVC 4? Rate this: Please Sign up or sign in to vote. See more: MVC Hi.. I am new to MVC 4. I am developing a simple application in MVC 4 using Razor in c#. Now i need to Display a error in view which is pass from controller? How can i achieve it? MY CODES: MY VIEW CODE: ------------- @model mvc_cs.Models.FormModels @using ctrlr = mvc_cs.Controllers.FormController @{ ViewBag.Title = "form_edit"; Layout = "~/Views/Shared/_Layout.cshtml"; }