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

  • Home
  • SEARCH
  • 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 4055450
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:42:10+00:00 2026-05-20T14:42:10+00:00

Maybe that’s not exactly the solution i need, but this is what i want

  • 0

Maybe that’s not exactly the solution i need, but this is what i want to do:

i have a company registration form, and each company needs an administrative user. an administrative user may manage multiple companies, so in the company registration form, you can choose an existing user from a dropdown.

the company view model looks something like this:

public class CompanyViewModel {
    [Required]
    public string Name { get; set; }
    // other properties...
    public UserViewModel Administrator { get; set; }
    public IEnumerable<UserViewModel> AvailableUsers { get; set; }
}

and the user view model looks like this:

public class UserViewModel {
    [Required]
    public string UserName { get; set; }
    [Required]
    public string Password { get; set; }
    // other properties...
}

in the company registration view:

<div><input type="radiobutton" name="chooseuser" id="existing"/>Choose an Existing User:</div.
<div>@Html.DropDownListFor(m => m.Administrator.Id, Model.AvailableUsers.Select(u => new SelectListItem { Text = string.Format("{0} - {1} {2}", u.UserName, u.FirstName, u.LastName), Value = u.Id.ToString() }), "<Choose existing user>", new { id = "existingusers" })
        </div>

<div><input type="radiobutton" name="chooseuser" id="createnew"/>Create a new User:</div>
<div><label>Username:</label> @Html.EditorFor(m => m.Administrator.UserName)</div>

Through javascript, based on radio button selection, the dropdown list is disabled and the new user form shown, or the new user form is hidden and the dropdown list is enabled.

The problem is in the controller Save action after you press save, ModelState.IsValid is false if an existing user is chosen and no data is filled in on the form. If the user chooses to enter a new user, validation succeeds.

What is the best way to handle this?

One option is to load all data for all users into data structures in javascript, and when the value changes on the existing user dropdown, the hidden “create new” form fields can be populated. But this seems lame since passwords would be sitting the html in plain text. i can get fancier and use ajax for a “create new” form and populate a user id on the original form once the new user is saved, but i’d like to keep it all in one form if possible.

Seems liked i’d ideally be able to load the existing user data from the db and populate the model state in the controller Save action, but writing this code manually (even using reflection) seems sloppy. It would be nice if there was a built in method to do this.

Any ideas?

Thanks.

  • 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-20T14:42:10+00:00Added an answer on May 20, 2026 at 2:42 pm

    That’s a typical scenario which perfectly illustrates the limitations of declarative validation (a.k.a Data Annotations). In order to handle it you could write a custom validation attribute which will be applied to the CompanyViewModel instead of individual properties and will allow you to perform the validation logic based on which radio button the user choose (btw you will need a property on your view model which will represent the radio button selection). The problem with model validators is that you might have some hard time handling the error highlighting.

    That’s one of the reasons why I use FluentValidation.NET instead of Data Annotations. This allows me to have the validation logic away from the model and done in an imperative way. It also allows me to have conditional validators which apply based on the values of some properties on the view model (in this case this would be the radio button selection).

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

Sidebar

Related Questions

maybe that problem is simple, but I have no Idea how to solve this,
I thought that there should have been a simple solution to this, given that
I realised this might be relatively niche, but maybe that's why this is good
Maybe that's a noob question but is there a way to have an yellow
I'm working on asp.net (vb, maybe that can change something) I have a project
If have an element that maybe disabled on a page, and would like to
I have a problem with preg_match_all that maybe you can help. I have a
I have to develop an appliction (Asp.Net - maybe MVC) that would be integrated
Imagine a path /root/child1/child2/child3 Imagine in zookeeper that maybe a part of this exists,
I'm trying to do multithread uploads, but get errors. I guessed that maybe it's

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.