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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:51:27+00:00 2026-06-19T03:51:27+00:00

I have two models that only differ by what fields are required, and I

  • 0

I have two models that only differ by what fields are required, and I would like to be able to use the same view to display both of them.

I have a UserEdit class

public abstract class UserEdit
{
    public User User { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string NewPassword { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [EqualTo("NewPassword")]
    public string ConfirmPassword { get; set; }

}

and a UserCreate Class that adds the required attribute to the NewPassword and ConfirmPassword properties.

class UserCreate_Metadata
{
    [Required]
    public string NewPassword { get; set; }

    [Required]
    public string ConfirmPassword { get; set; }
}

[MetadataType(typeof(UserCreate_Metadata))]
public partial class UserCreate : UserEdit
{

}

Both of these work fine in the view if I bind the model to the specific class like so:

@model Siaa.NextWeb.Data.UserEdit

or

@model Siaa.NextWeb.Data.UserCreate

Is there a way to make the view detect which class it is binding to so that it picks up the extra validation requirement of the UserCreate class? I tried using a dynamic view, but receive a “expression tree may not contain a dynamic operation” which research has pointed me to the lambda expressions used to create the editors

@Html.EditorFor(model => model.User.FirstName)
  • 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-06-19T03:51:28+00:00Added an answer on June 19, 2026 at 3:51 am

    The most intuitive way to do this would be to combine both models onto a single model that you always pass to the view. Then simply add some sort of flag on to the model that determines which one you should use.

    public class UserViewModel {
        public UserCreate CreateModel { get; set; }
        public UserEdit EditModel { get; set; }
    
        public bool IsCreatingUser { get; set; }
    }
    

    Then in your view:

    @if (Model.IsCreatingUser) {
        //Build your form.
    } else {
        //Build your form.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models that I would like to save in the same table.
I have two models that are linked by ForiegnKey, and I'd like to use
I have two models that I would like to present in a single Yii
I have a two models that are similar, but not exactly the same. Here's
I have two models User and Category that have a HABTM association. I would
I have two models User and Admin(with RailsAdmin) that use Devise. I sign in
I have two models, a MainModel and a related InlineModel that i'd like to
I have two models like that class Plan < ActiveRecord::Base belongs_to :profile And class
I have two models that are associated with each other. Customer has_one :primary_contact And
If I have two models that are very different, is it defeating the purpose

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.