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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:19:01+00:00 2026-05-26T01:19:01+00:00

I have a New user form both for admins and for regular users. Both

  • 0

I have a “New user” form both for admins and for regular users. Both form use the RegisterModel

public class RegisterModel
{
    [Required]
    public string Name { get; set; }

    public string Email { get; set; }

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

The difference is that on my front end “New user” page I want users to provide their own password. But in back end, I want the system to generate the password.

Since I use the same RegisterModel for both forms, I get a validateion error in the back end saying Password is required..

I thought, I could solve this by adding this to my controller:

    [HttpPost]
    public ActionResult New(RegisterModel model)
    {
        model.Password = Membership.GeneratePassword(6, 1);

        if (TryValidateModel(model))
        {
            // Do stuff
        }

        return View(model);
    }

But I still get the error message Password is required.. Why is this the issue when I do call TryValidate in my controller?

What would be best practice for this issue, create a separate RegisterModelBackEnd or are there any other solutions to this?

  • 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-26T01:19:02+00:00Added an answer on May 26, 2026 at 1:19 am

    When updating model manually, you do not need to use it as parameter in Action. Also, use this overload that lets you specify only the properties on which binding will occur.

    protected internal bool TryUpdateModel<TModel>(
        TModel model,
        string[] includeProperties
    )
    where TModel : class
    

    So, the working code will be

    [HttpPost]
    public ActionResult New()
    {
        RegisterModel model = new RegisterModel();
        model.Password = Membership.GeneratePassword(6, 1);
    
        if (TryValidateModel(model, new string[] {"Name", "Email"}))
        {
            // Do stuff
        }
    
        return View(model);
    }
    

    You can make this even simpler, using BindAttribute

    [HttpPost]
    public ActionResult New([Bind(Exlude="Password")]RegisterModel model)
    {
        if(ModelState.IsValid)
        {
           model.Password = Membership.GeneratePassword(6, 1);
           // Do Stuff
        }
    
        return View(model);
    }
    

    And finally simplest and the best way

    Define separate view models

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

Sidebar

Related Questions

I have got a form which a user can use to create a new
I have following code class User attr_accessor :name end u = User.new u.name =
I have to start a new project where user authentication/management will be required. A
I have this block of code: users = Array.new users << User.find(:all, :conditions =>
So I have a form that lets a user create a new team. Our
I'm new to both Silverlight and RIA. I have a simple form with a
I'm trying to use Method.Invoke to call a windows form dialog, have a user
I have such view that handles user registration. After creating new user i want
Hello I have a user model and a ratings model. Whenever a new user
I have a database called ADB. When I create a new user/role, that user

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.