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 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

Having not done ASP.NET since v1.1, and now blitzing through the Wrox Pro ASP.NET
I am considering buying an Apple MacBook Pro. Are there any pitfalls developing C#/.NET
WinXP Pro Oracle 10g Instant Client 10.2.0.1 MS Access 2003 When I link a
In VisualStudio (Pro 2008), I have just noticed some inconsistent behaviour and wondered if
How do you get Pro*c to work within MSVC 6? In otherwords compile a
What are the pro/cons of doing web development on your local machine rather than
What does Pro have that's not in Standard (or vice-versa)? I need Visual Studio
I develop using MAMP pro on my Mac. When I start MAMP it prompts
A website I maintain pro-bono was hacked, dishing out 302s to gaming sites, etc.
I have just got a MacBook Pro and have been using it (+Fusion) to

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.