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

  • Home
  • SEARCH
  • 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 1072689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:53:47+00:00 2026-05-16T20:53:47+00:00

On PRO ASP.NET MVC book: It is certainly possible to put domain logic into

  • 0

On PRO ASP.NET MVC book:

It is certainly possible to put domain
logic into a controller, even though
you shouldn’t, just because it seems
expedient at some pressured moment.

Just a contrived example, if the application doesn’t allow negative order, where to put the changing of quantity to 1? If we follow the principle that domain logic shouldn’t be placed in controller, this is certainly not advisable to use:

[AcceptVerbs(HttpVerbs.Post)]        
public ActionResult PlaceOrder(Order order)
{
 if (ModelState.IsValid)
 {
  order.Submit();
  return View("Thanks", order);
 }
 else
 {
    if (order.Quantity <= 0)
    {
        ModelState.Remove("Quantity");
        order.Quantity = 1;  
    }
    return View(order);
 }
}

So the following code is the right code that adheres to MVC principle, i.e. it follows separation of concerns, if it’s domain logic you should not see its code in controllers. So this is how I tried placing the domain logic in Model:

public class Order : IDataErrorInfo
{
 public int OrderId { set; get; }
 public int ProductId { set; get; }
 public int Quantity { set; get; }

 public string Error { get { return null; } }

 public string this[string propName]
 {
  get
  {
   if (propName == "Quantity" && Quantity <= 0)
   {
    Quantity = 1;
    return "0 or negative quantity not allowed, changed it to 1";
   }
   else
    return null;

  }
 }
}

Controller (sans the domain logic):

[AcceptVerbs(HttpVerbs.Post)]        
public ActionResult PlaceOrder(Order order)
{
 if (ModelState.IsValid)
 {
  order.Submit();
  return View("Thanks", order);
 }
 else
 {
  // Response.Write(order.Quantity.ToString()); // this was changed in Model
  return View(order); // but the View didn't reflect that fact
 }
}

The only problem with that approach, the Model(Order) can’t influence the ModelState, and such, the program always display whatever is last entered by the user.

What’s the best approach so I can still avoid placing domain logic in controller and the View is still able to reflect the values of Model’s properties?

  • 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-16T20:53:47+00:00Added an answer on May 16, 2026 at 8:53 pm

    Validation is not controllers task. You can Put all required logic in different module, and just propogate requests there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently reading the book Pro Asp.Net MVC Framework. In the book, the author
I was reading Steven Sanderson's book Pro ASP.NET MVC Framework and he suggests using
I am working through Steve Sanderson's book Pro ASP.NET MVC Framework and I having
I've been working through the book Pro ASP.NET MVC 2 Framework by Steven Sanderson.
I've been learning the ASP.NET MVC framework using the Apress book Pro ASP.NET MVC
I reading Steven Sanderson's book, Pro Asp.NET MVC 2 Framework. I'm on page 116
After reading Steven Sanderson's book Pro ASP.NET MVC I am convinced about the benefits
Working through Pro ASP.NET MVC book and I got the following code snippet that
I was following Steven Sanderson's ' Pro ASP.NET MVC Framework ' book. On page
I'm trying to implement Steven Sanderson's WinsorControllerFactory from his pro Asp.Net MVC Framework book

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.