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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:43:10+00:00 2026-05-30T13:43:10+00:00

I have 2 view models like this: public class ViewModel1 // maps to Model1

  • 0

I have 2 view models like this:

public class ViewModel1 // maps to Model1
{
    public string ViewModel1Desc { get; set; }
    public ViewModel2 ViewModel2 { get; set; }
    public ScheduleMasterEditViewModel()
    {
        ViewModel2= new ViewModel2();
    }
}

public class ViewModel2 // maps to Model2
{
    public string ViewModel2Desc { get; set; }
}

Now, I wanted to have a partial page for ViewModel2 and include that in the create page for ViewModel1:

Create.cshtml looks something like this

@model ViewModels.ViewModel1
@using (Html.BeginForm()) {
    @Html.EditorFor(model => model.ViewModel1Desc )
    @Html.Partial("~/Views/ViewModel2/_ViewModel2Create.cshtml", Model.ViewModel2)
}

_ViewModel2Create.cshtml looks like

@model ViewModels.ViewModel2
@Html.EditorFor(model => model.ViewModel2Desc )

The problem is, on the Create controller for Model1, nothing gets bound to ViewModel1.ViewModel2

Am I doing this the right way, or should I just write out all the fields like 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-30T13:43:11+00:00Added an answer on May 30, 2026 at 1:43 pm

    The reason your ViewModel2 is not bound is because when you look at the generated HTML you will notice that the input fields that were created for this submodel have incorrect names:

    <input type="text" name="ViewModel2Desc" />
    

    whereas the correct is:

    <input type="text" name="ViewModel2.ViewModel2Desc" />
    

    The reason for this is that your _ViewModel2Create.cshtml partial doesn’t keep the navigational property context of the parent.

    For this reason I would recommend you to use editor templates instead of a partial call:

    @model ViewModels.ViewModel1
    @using (Html.BeginForm()) {
        @Html.EditorFor(model => model.ViewModel1Desc)
        @Html.EditorFor(model => model.ViewModel2)
    }
    

    and then move your partial code inside ~/Views/Shared/EditorTemplates/ViewModel2.cshtml:

    @model ViewModels.ViewModel2
    @Html.EditorFor(model => model.ViewModel2Desc)
    

    Notice the location of the editor template: ~/Views/Shared/EditorTemplates. This is important. It is where ASP.NET MVC will look for it. In fact it will first look in ~/Views/XXX/EditorTemplates where XXX is your current controller name for more specific templates and if it doesn’t find one look in the Shared folder. Also notice the name of the file: ViewModel2.cshtml. This also is important and it works by convention. The name of the template is actually the type of the property.

    You could override this:

    @Html.EditorFor(model => model.ViewModel2, "_ViewModel2Create")
    

    or using an UIHint attribute on your view model:

    public class ViewModel1
    {
        public string ViewModel1Desc { get; set; }
        [UIHint("_ViewModel2Create")]
        public ViewModel2 ViewModel2 { get; set; }
        public ScheduleMasterEditViewModel()
        {
            ViewModel2 = new ViewModel2();
        }
    }
    

    and then have ~/Views/Shared/EditorTemplates/_ViewModel2Create.cshtml.

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

Sidebar

Related Questions

I have public class ViewModel1 { // some properties here public List<ViewModel2> ViewModel2 {get;
I have a ViewModel that looks like this: public class CreateReviewViewModel { public string
I have a class that roughly looks like this: public class ViewModel { public
I have a ViewModel as such: public class EmployeeViewModel { Employees employee{ get; set;
Suppose I have this model: public class ViewModel { [Required] public string UserInput {
If i have a ViewModel like this: public class MyViewModel { [UIHint(SomeTemplate)] public ICollection<SomeViewModel>
I have a model that looks something like this: public class SampleModel { public
I have an ASP.NET ViewModel like this: public class ParentViewModel { public ChildViewModel Child
I have a view model public class DeviceModelEntryViewModel { public int ID { get;
I have View Model with inner ViewModel Address propery. public class CommonViewModel{ public AddressViewModel

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.