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

The Archive Base Latest Questions

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

I have a domain model and a view model as follows: Domain Model: namespace

  • 0

I have a domain model and a view model as follows:

Domain Model:

namespace MvcApplication1.Models
{
    public enum Sex { Male, Female };
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }

        [Required(ErrorMessage="Please select either Female or Male.")]
        public Sex? Sex { get; set; }
    }
}

View Model:

namespace MvcApplication1.ViewModels
{
    public class HomeCreateVM
    {
        public HomeCreateVM()
        {
        }
        public HomeCreateVM(Person p)
        {
            Person = p;
            SelectList = p.Sex.GetSelectList();
        }
        public Person Person { get; set; }
        public SelectList SelectList { get; set; }

    }
}

The auxiliary extension method is defined as follows:

namespace MvcApplication1.Models
{
    public static class Utilities
    {
        public static SelectList GetSelectList<XXX>(this XXX? obj) where XXX : struct
        {
            var values = from XXX x in Enum.GetValues(typeof(XXX))
                         select new { Text = x.ToString(), Value = x };
            return new SelectList(values, "Value", "Text", obj);
        }
    }
}

Controller:

public ActionResult Create()
{
    var p = new Person();

    return View(new HomeCreateVM(p));
}

[HttpPost]
public ActionResult Create(Person hc)// the source of problem!
{
    if (ModelState.IsValid)//always false!
    {
        TempData["status"] = hc;
        return RedirectToAction("Confirm");
    }
    else
        return View(new HomeCreateVM(hc));
}

HomeCreateVM.cshtml:

@model MvcApplication1.ViewModels.HomeCreateVM
<div>
    Name: @Html.EditorFor(model => model.Person.Name)</div>
<div>
    Sex: @Html.DropDownListFor(model => model.Person.Sex, Model.SelectList, "--Select--")</div>

Create View:

@model MvcApplication1.ViewModels.HomeCreateVM

@{
    ViewBag.Title = "Create";
}

<h2>Create</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()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>HomeCreateVM</legend>
        @Html.EditorForModel()
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

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

Question:

There is no problem if the POST Create action method accepts a HomeCreateVM object as the argument.
However, if I change the POST Create action method argument from HomeCreateVM to Person (as shown in the code above), ModelState.IsValid always returns false.

The question is: “Is it possible to pass a ViewModel object to a Create view but only accept a DomainModel object from a POST Create action method?”

  • 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-19T17:26:38+00:00Added an answer on May 19, 2026 at 5:26 pm

    Because your view is strongly typed to the view model your form fields will look like this:

    <input type="text" name="Person.Name" />
    

    and if you want to bind correctly you need to specify the prefix:

    [HttpPost]
    public ActionResult Create([Bind(Prefix = "Person")]Person hc)
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my domain model I have an abstract class CommunicationChannelSpecification, which has child classes
I have a class in my domain model called JobPlan this class is stored
I have the following models. # app/models/domain/domain_object.rb class Domain::DomainObject < ActiveRecord::Base has_many :links_from, :class_name
So I have a controller set up as follows: using NonStockSystem.Models; namespace NonStockSystem.Controllers {
We have our domain model declared in rusty old hbm files, we wish to
We have a simple domain model: Contact, TelephoneNumber and ContactRepository. Contact is entity, it
So, I have a nice domain model built. Repositories handle the data access and
I have an application A with a domain-model which is mapped to a database
While fleshing out a hypothetical domain model, I have found myself wondering whether the
We're using interfaces to represent entity classes in our domain model. We have concrete

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.