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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:15:06+00:00 2026-06-07T02:15:06+00:00

I’m new to MVC and am unsure about proper design. I have class objects

  • 0

I’m new to MVC and am unsure about proper design.

I have class objects which I use in a variety of applications. I have taken the approach to write a custom view model class so that I can have access to properties in all of these objects and have strong typing. Without re-typing all my class code in the view model is there any way to have the properties in these objects validated using data annotation? Please let me know if my approach and design is all wrong.

[Required]        
public User user = new User("username");
//User has lots properites and methods, could i validate inside my class code?

//What I’d like to avoid is putting the following stuff in my custom view model class, //since I already have a class library with this stuff:

public class User
{

    [Required]
    [StringLength(160)]
    public string prop1 { get; set; }
    [Required]
    [StringLength(160)]
    public string prop2 { get; set; }
    [Required]
    [StringLength(160)]
    public string prop3 { get; set; }

    public User(string token)
    {
        SetUser(token);
    }


    public void SetUser(string token)
    {
        this.prop1 = "this";
        this.prop2 = "this2";
        this.prop3 = "this3";

    }

============
Good to know I can, but I’m stumbling on some issues. In my view I have: @Html.EditorFor(modelItem => modelItem.user.prop1)

I put the data annotation stuff in my class domain. When it renders it does show the annoations.

<input class="text-box single-line" data-val="true" data-val-length="The field prop1 must be a string with a maximum length of 5." data-val-length-max="5" data-val-required="The prop1 field is required." id="user_prop1" name="user.prop1" type="text" value="somevalue" />

but when I go to my controller the parameter is null. I think because the name is user.prop1. I tried a textbox where i specified the name attribute and yet my controller still couldn’t get a value for my parameter.

====================

            @model TrainingCalendar.Models.Training

            @{
                ViewBag.Title = "Signup";
            }

            <h2>Signup</h2>

            <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
            <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

            @using (Html.BeginForm("ConfirmSignup", "Training", FormMethod.Post))
            {
                @Html.ValidationSummary(true)
                <fieldset>
                    <legend>Training</legend>
                    <p>
                    @Html.Label("date", Model.SpecifiedCourse.strClassDate)
                    </p>
                    <p>
                    @Html.Label("time", Model.SpecifiedCourse.Time)
                    </p>
                    <p>
                    @Html.Label("instructor", Model.SpecifiedCourse.Instructor)
                    </p>
                    <p>
                    @Html.Hidden("id", Model.SpecifiedCourse.ID)
                    </p>
                    <table>
                        <tr>
                            <td>@Html.LabelFor(modelItem => modelItem.ApplicationUser.prop1)</td>
                            <td>@Html.EditorFor(modelItem => modelItem.ApplicationUser.prop1)</td>
                            <td style="color:Red">@Html.ValidationMessageFor(modelItem => modelItem.ApplicationUser.prop1)</td>
                        </tr>
                        <tr>
                            <td>@Html.LabelFor(modelItem => modelItem.ApplicationUser.prop2)</td>
                            <td>@Html.EditorFor(modelItem => modelItem.ApplicationUser.prop2)</td>
                            <td style="color:Red">@Html.ValidationMessageFor(modelItem => modelItem.ApplicationUser.prop2)</td>
                        </tr>

                    </table>   
                    <p>
                        <input type="submit" value="Sign Up" />
                    </p>
                </fieldset>
            }

            <div>
                @Html.ActionLink("Back to List", "Index")
            </div>

===================

            public ActionResult ConfirmSignup(
                        int id,
                        string prop1,
                        string prop2)
                    {
                        SignUpForClass();
                    return View();
                }
  • 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-07T02:15:07+00:00Added an answer on June 7, 2026 at 2:15 am

    Absolutely you can have the data annotation attributes on your class code. If you’re encapsulating the class in your view model, populated the properties of the encapsulated class in your view. During the validation process the class members will be validated against the data annotation attributes that you specify in the class declaration.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have a text area in my form which accepts all possible characters from
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.