Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7698589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:16:39+00:00 2026-05-31T22:16:39+00:00

In my MVC3 application, I have a queries class specific to each controller that

  • 0

In my MVC3 application, I have a “queries” class specific to each controller that performs conversions between domain entities and converts them to view models. I do this to keep my controllers clean and it’s easier to unit test the controllers and queries seperately.

There are cases, though, when the query method needs to pass non-exceptional error messages to the view (e.g. the entity was not found). However, since my controller is only receiving a ViewModel from the query method and not any type of return code, the only two options that I have found to pass this error are as follows:

  1. Throw an Exception from the query method and use a Try/Catch block to catch the exception in the controller.

  2. Add a property to the View Model called “ErrorMessage” which is populated by the query method that the view uses to perform logic of what needs displayed.

Because these aren’t exceptional cases, and I know I shouldn’t use Try/Catch to control program flow, I have choosen to use the second method. Though this works for now, it feels “dirty” to me for the following reasons:

  • The controller has to receive a whole View Model when an error occurs just to get the ErrorMessage property.
  • The view has to have hard-coded logic and two sections to display either the error or the normal content
  • Though I could put the if (Model.ErrorMessage != null) logic in my controller to determine which view to pass, it’s still not feeling like a “clean” solution.

Are there any design patterns that I could use that could help me refactor this code and make it cleaner?

Example View Model:

public class ApplicationViewModel
{
  public string ErrorMessage { get; set; }
  public int Id { get; set; }
  public string Name { get; set; }
  // Other properties here...
}

Example Controller Method:

public ActionResult Retrieve(Guid guid)
{
  return View("Application", _applicationQueries.GetApplicationViewModel(guid));
}

Example ApplicationQueries Method:

public ApplicationViewModel GetApplicationViewModel(Guid guid)
{
  var applicationViewModel = new applicationViewModel();

  if (!_applicationServices.Exists(guid))
  {
    applicationViewModel.ErrorMessage = "The requested application does not exist.";
    return applicationViewModel;
  }

  // More code here that checks things which might set the ErrorMessage property...

  var application = _applicationServices.GetApplicationByGuid((Guid)guid);
  Mapper.Map(grantApplication, grantApplicationViewModel);
  return grantApplicationViewModel;
}

Snippit from Application.cshtml View for Error Handling:

@model MyApp.Web.Areas.Application.Models.ApplicationViewModel
if (Model.ErrorMessage != null)
{
  <div>@Model.ErrorMessage</div>
}
else
{
  <!-- Display "normal" content here //>
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:16:40+00:00Added an answer on May 31, 2026 at 10:16 pm

    You could pass the ModelState instance to your queries layer and it will take care to add the error:

    public ApplicationViewModel GetApplicationViewModel(Guid guid, ModelStateDictionary modelState)
    {
        var applicationViewModel = new applicationViewModel();
        if (!_applicationServices.Exists(guid))
        {
            modelState.AddModelError("", "The requested application does not exist.");
            return applicationViewModel;
        }
    
        // More code here that checks things which might set the ErrorMessage property...
    
        var application = _applicationServices.GetApplicationByGuid((Guid)guid);
        Mapper.Map(grantApplication, grantApplicationViewModel);
        return grantApplicationViewModel;
    }
    

    and in your view:

    @model MyApp.Web.Areas.Application.Models.ApplicationViewModel
    
    @Html.ValidationSummary()
    
    @if (ViewData.ModelState.IsValid)
    {
        <!-- Display "normal" content here //>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my ASP.NET MVC3 application I have the following method that use EF entities
I have an MVC3 application that has a custom HandleErrorAttribute so that I can
MVC3 vb.net Application using built in session management. I have an mvc3 application that
I have an issue with sessions in an MVC3 application. In one controller I
I have an MVC3 application that I am implementing pjax into . Everything is
I have an MVC3 application that supports multiple languages. Do I need to create
I have lots of forms in my MVC3 application that require the user to
I have an ASP MVC3 application that runs fine (in VS 2010 debug) in
I have an MVC3 application, I know that for relative paths, I can use
We have a MVC3 application that we have created many small actions and views

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.