Asp.net Mvc Displaying Error Messages
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
Aspnet Mvc Nuget
this site About Us Learn more about Stack Overflow the company Business Learn aspnet mvc source more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question
Aspnet Mvc Tutorial
x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up aspnet mvc 5 MVC 4-Return error message from Controller -Show in View up vote 15 down vote favorite 4 I 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"); aspnet mvc 4 } 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) @Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--") |
---|
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
Aspnet Mvc Cms
Ask a Question View Unanswered Questions View All Questions... C# questions aspnet mvc 6 Linux questions ASP.NET questions SQL questions VB.NET questions discussionsforums All Message Boards... Application Lifecycle> Running a Business Sales
Aspnet Mvc 3
/ 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 http://stackoverflow.com/questions/20141102/mvc-4-return-error-message-from-controller-show-in-view and Swift Database Hardware & 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 http://www.codeproject.com/Questions/679815/How-to-Display-a-error-message-in-view-from-contro The Lounge The Insider News 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"; }
form_edit
MY CONTROLLER: -------------- [HttpPost] public ActionResult form_edit(FormModels model) { model.error_msg = model.update_content(model); TempData["error"] = "fjghsfhgflsh"; ModelState.AddModelError("error.error", "adfdghdghgdhgdhdgda"); return RedirectToAction("Form_edit", "Form",model.error_msg); } Posted 7-Nov-13 20:35pm srameshsathy277 Uphere 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 the company Business Learn more about hiring developers or posting ads with us Programmers Questions Tags Users Badges Unanswered Ask Question _ Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top MVC: Where is the right place to set error messages? up vote 3 down vote favorite 1 I have a ASP.NET MVC application where in all my Models have an Errors Property used to store non-validation errors which I than display in my Views. Where is the right place to populate the error list according to MVC? Inside the model or inside the controller? First Option public class MyModel { public DataOperationResult DoSomething() { //do something with data if (result != DataOperation.Success) { Errors.Add("Something went wrong"); } return result; } } Second option public class MyController { public ActionResult DoSomething() { MyModel model = new myModel(); DataOperationResult result = model.DoSomething(); if (result != DataOperation.Success) { model.Errors.Add("Something went wrong"); } } } c# mvc error-handling asp.net-mvc-4 share|improve this question edited Sep 22 '14 at 12:44 asked Sep 22 '14 at 10:58 Georgi Georgiev 1858 Are the errors occurring inside the model, or in the controller? –Robert Harvey Sep 23 '14 at 15:13 @RobertHarvey Mostly the errors are occurring in the model, different DataOperationResults are defined for different error types and depending on the error type the controller redirects to different pages –Georgi Georgiev Sep 24 '14 at 6:32 add a comment| 1 Answer 1 active oldest vo