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 6385775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:56:30+00:00 2026-05-25T02:56:30+00:00

I’ve been developing a web app using asp.net mvc and nhibernate. I’m trying to

  • 0

I’ve been developing a web app using asp.net mvc and nhibernate. I’m trying to follow some principles of DDD and best pratices of Asp.Net MVC. My question is about Clean up POST with VIewModels. To illustrate my question, look this entity on my domain model:

[Validator(typeof(EntityValidator))]
public class MyEntity {
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Email { get; set; }
    public virtual decimal UnitPrice { get; set; }
    public virtual Category Category { get; set; }

    public MyEntity() { }
}

It’s mapped with nhibernate and works fine. For validation, I’m using Fluent Validation, and I have this class:

public class EntityValidator : AbstractValidator<MyEntity>
{   
    protected IEntityRepository EntityRepository { get; set; }
    public EntityValidator(IEntityRepository entityRepository) { // injected by a IoC Container
        this.EntityRepository = entityRepository;

        RuleFor(x => x.Name).NotEmpty();
        RuleFor(x => x.UnitPrice).GreaterThan(0);
        RuleFor(x => x.Category).NotNull();
        RuleFor(x => x.Email).NotEmpty().EmailAddress().Must((entity, email) => EntityRepository.ExisteEmail(entity.Email));
    }
}

I preffer to use Fluent Validation than Data Annotations because it’s more flexible and works fine with Asp.Net MVC. It’s configured and works fine too. So, I have this ViewModel:

public class EntityViewModel {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public decimal UnitPrice { get; set; }
    public int IdCategory { get; set; }
}

Now, I’m trying to do a ViewModel to make a clean up POST of my entity in some actions (like INSERT, UPDATE) because I’m using nhibernate. I

don’t know if it’s right to create a validation for viewmodel, because I have my own on my entity, so, how can I do this? How have you been doing

to POST your entity on actions to persist it? How you validate it and post erros on ModelState of MVC?

I’d like to see some code of how to do it, if it’s possible.

Thanks everyone!

  • 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-25T02:56:31+00:00Added an answer on May 25, 2026 at 2:56 am

    don’t know if it’s right to create a validation for viewmodel, because
    I have my own on my entity, so, how can I do this?

    Personally I define fluent validators for my view models. They contain validation rules for simple things like required properties, formats, etc… so that those validation could be handled directly on the view.

    Domain models could also contain validations but those will be business validations. Here’s the usual flow of a POST controller action:

    [HttpPost]
    public ActionResult Insert(UpdateViewModel viewModel)
    {
        if (!ModelState.IsValid)
        {
            // the surface validation on our view model failed => redisplay the view so
            // that the user can fix errors
            return View(viewModel);
        }
    
        // at this stage the view model is valid => we can map it back to a domain model
        // I use AutoMapper for this:
        var domainModel = Mapper.Map<UpdateViewModel, DomainViewModel>(viewModel);
    
        // then we pass the domain model to the service layer for processing:
        string error;
        if (!_service.Insert(domainModel, out error))
        {
            // something wrong happened on the service layer
            ModelState.AddModelError("key", error);
            return View(viewModel);
        }
    
        // everything went fine
        return RedirectToAction("Success");
    }
    

    As you can see from this example we are delegating the processing to the service layer. The way this service is implemented is out of any interest or meaning for an ASP.NET MVC application. You could be using NHibernate, Entitiy Framework, call some other services on the could, anything you could think of, all that we should care is that we are handling our domain model ready for being processed and it reports us back errors if any. The way you handle your validation on the service layer is also out of any importance for the ASP.NET MVC application => you could be using Castle Validator, Fluent Validation, Data Annotations, whatever …

    As far as the mapping is concerned there could be an additional step if for example you were updating an existing domain entity. In this case the view model probably doesn’t contain all the properties of the domain model because in this specific view for example we allow for editing only some of the properties. In this case the flow would be like this:

    // Fetch the original domain model we want to update
    var domainModel = _service.Get(viewModel.Id);
    
    // Update only the properties that are present on the view:
    Mapper.Map<UpdateViewModel, DomainViewModel>(viewModel, domainModel);
    
    // Pass to the service layer for processing:
    string error;
    if (!_service.Update(domainModel, out error))
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.