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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:41:31+00:00 2026-05-12T13:41:31+00:00

Suppose I want to allow to select our entity (from a dropdown, etc) on

  • 0

Suppose I want to allow to select our entity (from a dropdown, etc) on a page, let’s say Product. As a result I may receive this:

public ActionResult SelectedAction(Guid productId)
{
}

But, I want to use model binders power, so instead I write model binder to get my product from repository and instead use

public ActionResult SelectedAction(Product product)
{
   if (ModelState.IsValid) {} else {}
}

My model binder will set model state to false if product is invalid.
Now, there’re problems with this approach:

  1. It’s not always easy to use strongly-typed methods like Html.ActionLink(c => c.SelectedAction(id)) since we need to pass Product, not id.
  2. It’s not good to use entities as controller parameters, anyway.
  3. If model state is invalid, and I want to redirect back and show error, I can’t preserve selected product! Because bound product is not set and my id is not there. I’d like to do RedirectToAction(c => c.Redisplay(product)) but of course this is not possible.

Now, seems like I’m back to use “Guid productId” as parameter… However, there’s one solution that I’d like to present and discuss.

   public class EntityViewModel<T> where T : BaseEntity
   {
      public EntityViewModel(Guid id)
      {
         this.Id = id;
      }

      public static implicit operator EntityViewModel<T>(T entity)
      {
         return new EntityViewModel<T>(entity.Id);
      }

      public override string ToString()
      {
         return Id.ToString();
      }

      public Guid Id { get; set; }
      public T Instance { get; set; }
   }

Now, if I use

public ActionResult SelectedAction(EntityViewModel<Product> product)
{
   if (ModelState.IsValid) {} else {}
}

all the problems are solved:

  1. I can pass EntityViewModel with only Id set if I have only Id.
  2. I don’t use entity as parameter. Moreover, I
    can use EntityViewModel as property inside another ViewModel.
  3. I can pass EntityViewModel back to RedirectToController and it will keep its Id value, which will be
    redisplayed to user along with the validation messages (thanks to MVCContrib and ModelStateToTempData / PassParametersDuringRedirect).

The model binder will get Instance from the repository and will set model state errors like “Not found in database” and so on. And I can use things like ActionLink(c => c.Action(Model.MyProductViewModelProperty)).

The question is, are there any drawbacks here? I can’t see anything bad but I’m still new to MVC and may miss some important things. Maybe there’re better and approved ways? Maybe this is why everybody uses entity IDs as input parameters and 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-12T13:41:31+00:00Added an answer on May 12, 2026 at 1:41 pm

    Overall that looks like a good appoach to me…

    As an alternative, you could use POCO for your viewmodel then I think all 3 problems would be solved automatically. Have you seen the Automapper project that allows an Entity to DTO approach? This would give you more flexibility by separating you ViewModel from your EntityModel, but really depends on the complexity of you application you are building.

    MVC’s ViewDataExtensions might also be useful instead of creating custom containers to hold various viewmodel objects as you mention in number 2.

    MVCContrib’s ModelStateToTempData should work for any serializable object (must be serializable for any out of process sessionstate providers eg. SQL, Velocity etc.), so you could use that even without wrapping your entity classes couldn’t you?

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

Sidebar

Related Questions

Suppose I want to add two buffers and store the result. Both buffers are
I want to allow bolding, italic ,underlines ,text-colors, etc in a UITextField. Is there
Suppose I have the classes/models Projects (has many lists) Lists I want to allow
Suppose you have a lightbox, and you want to allow the user to middle-click,
Suppose I want to create an Uri object from the following string: string url
I want to allow my users to update their account details. To simplify, suppose
Suppose I want to store N samples (each sample takes up a significant portion
Suppose I want to have a blog with Rails 3 on my website and
Suppose I want to put objects that identify a server into a stl set
Suppose I want to execute code, for example value += 5 inside a namespace

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.