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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:21:31+00:00 2026-05-24T21:21:31+00:00

Hi just thought about mapping and binding my entity in controller. How should i

  • 0

Hi just thought about mapping and binding my entity in controller.
How should i correctly bind entity in model so i can use modelstate

I am creating new MenuItem using MenuItemModel.

public class MenuItemModel
{
    public List<SelectListItem> Menus { get; set; }
    public MenuItem MenuItem { get; set; }
}

where my MenuItem class is defined as follows:

public class MenuItem:Entity
{

    public virtual int MenuItemId { get; set; }
    public virtual Menu Menu { get; set; }
    [Required]
    public virtual string Name { get; set; }
    public virtual int ItemOrder { get; set; }
    public virtual string ExternalUrl { get; set; }
    public virtual DateTime Created { get; set; }
    public virtual bool Deleted { get; set; }
    public virtual DateTime? DisplayUntil { get; set; }
    public virtual User Author { get; set; }

}

now when i bind my entity in controller.

    //
    // POST: /Administrator/MenuItem/Create

    [HttpPost]
    public ActionResult Create(MenuItem menuItem)
    {
        if (ModelState.IsValid)
        {
            // do saving logic
        menuItem.Created = DateTime.Now;
        menuItem.Author = this._userProvider.GetCurrentUser();
        menuItem.Menu = _menuRepository.Load(menuItem.Menu.MenuId);

        }
        //restore
        MenuItemModel menuItemModel = new MenuItemModel();
        menuItemModel.MenuItem = menuItem;
        menuItemModel.Menus =
            this._menuRepository.All.Select(x => new SelectListItem() { Text = x.Name, Value = x.MenuId.ToString() }).ToList();

        return View(menuItemModel);
    }

the only problem is i am getting validation not for only MenuItem but for Menu, User too.

How shall set this validation to accept validation only for MenuItem Entity ?

PS i know that i can go into modelstate items and find only the entities that i need and check if they are valid but i believe there will be better way of doing this…

Any idea is appreciated.

  • 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-24T21:21:34+00:00Added an answer on May 24, 2026 at 9:21 pm

    How shall set this validation to accept validation only for MenuItem Entity ?

    You should use a view model which contains only the properties that are needed to be validated in your controller action (usually those are the properties contained on the form and entered by the user). View models are classes which are specifically designed for the requirements of a given view. A controller action should never pass/take a domain model to/from a view. A controller action should always pass/take a view model to/from a view. For example:

    public class MenuItemViewModel
    {
        public int MenuItemId { get; set; }
    
        [Required]
        public string Name { get; set; }
    
        ... put any properties that are contained on the form with their
            respective validation
    }
    

    then have your POST controller action take this view model as argument:

    [HttpPost]
    public ActionResult Create(MenuItemViewModel viewModel)
    {
        if (!ModelState.IsValid)
        {
            // there were some validation errors => redisplay the view
            // so that the user can fix them
            return View(viewModel);
        }
    
        // at this stage validation went fine
        // TODO: map the view model back to a domain model 
        // (MenutItem or whatever you are willing to update)
        // I would recommend you AutoMapper for this task: http://automapper.codeplex.com/
    
        // TODO: once you get the domain model pass it to a service layer
        // method in order to perform the necessary business operation with it
        // (in your case creating a menu item)
    
        return RedirectToAction("Success");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just thought about it. Is there a semantic distinction, or are we free to
I just thought about converting one of our custom controls to WPF, however it
I probably just haven't thought this through, or perhaps I'm simply unaware of an
I was just putting some thought into different languages (as I'm reviewing for final
OK thought I understood IDipose but just reading the best practices section of Accelerated
Just listening to this week's podcast and thought it would be nice to group
I thought to be clever and just put an transparent UIButton over an UIImageView
I thought that layout is just a widget that keeps more widgets inside. But
I thought that to clone a List you would just call: List<int> cloneList =
I thought an variable in objective-c is just a reference to an object somewhere

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.