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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:42:50+00:00 2026-06-16T19:42:50+00:00

I have a view model with a single child entity and various other properties.

  • 0

I have a view model with a single child entity and various other properties.

In my view I want to display a form for the child entity.
Using the following code:

@Html.HiddenFor(model => model.Item.ItemID)

produces the following output:

<input id="Item_ItemID" name="Item.ItemID" type="hidden" value="234" />

As you can see, the Html helper has prefixed the id and name attributes, whereas I would have expected the output to be

<input id="ItemID" name="ItemID" type="hidden" value="234" />

Consequently, the former output causes an error when the form is submitted because the form elements do not correspond to properties on the child entity.

I know I can get around this by hard-coding the hidden field

<input id="ItemID" name="ItemID" type="hidden" value="@Model.Item.ItemID" />

which kind of defeats the reason for having Html helpers,
or creating a partial view and passing in the child object

@{Html.RenderPartial("ItemForm", Model.Item);}

I am aware of being able to pass in html attributes to the method, and also writing my own extension method, but things get more complicated when data validation and jQuery are involved i.e the following code:

@Html.EditorFor(model => model.Item.Title)
@Html.ValidationMessageFor(model => model.Item.Title)

produces this code:

<input class="text-box single-line" data-val="true" data-val-required="The Title field is required." id="Item_Title" name="Item.Title" type="text" value="Some text" />
<span class="field-validation-valid" data-valmsg-for="Item.Title" data-valmsg-replace="true"></span>

so there needs to be an elegant method to keep the property names in synch.

So can anyone answer why does an HtmlHelper add a prefix to attributes for a child item of a view model?
And as a follow up question, is there any other neat way of preventing the prefix from being added?

  • 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-16T19:42:51+00:00Added an answer on June 16, 2026 at 7:42 pm

    HTML helpers are built with model binding conventions in mind. So for example I suppose that you have the following model:

    public class MyViewModel
    {
        public ItemViewModel Item { get; set; }
    
        // ... other properties
    }
    
    public class ItemViewModel
    {
        public int ItemID { get; set; }
    }
    

    and the corresponding view is strongly typed to MyViewModel.

    So when you use:

    @Html.HiddenFor(model => model.Item.ItemID)
    

    the helper generates:

    <input id="Item_ItemID" name="Item.ItemID" type="hidden" value="234" />
    

    because that’s what would allow you to properly bind your view model in the corresponding controller action when the form is POSTed back to the server:

    [HttpPost]
    public ActionResult Process(MyViewModel model)
    {
        // model.Item.ItemID will be correctly bound here from the hidden field value
    }
    

    I guess that your problems are coming from the fact that your controller action is taking an ItemViewModel as parameter instead of MyViewModel:

    [HttpPost]
    public ActionResult Process(ItemViewModel model)
    {
        // model.ItemID will not be correctly bound here from the hidden field value
    }
    

    So you could use the [Bind] attribute and specify a prefix to help the model binder:

    [HttpPost]
    public ActionResult Process([Bind(Prefix="Item")] ItemViewModel model)
    {
        // model.ItemID will be correctly bound here from the hidden field value
    }
    

    or simply design another view model stripping all the unnecessary properties from it and leaving only the Item property. Now your controller action could take this specifically designed view model.

    So as you can see at the end of the day you could perfectly fine control the model binding if you design your view models correctly. It’s all about view models in ASP.NET MVC. They solve all problems.

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

Sidebar

Related Questions

I have three view model bindings attached to a single page using the following
I have a ASP.Net MVC form that is bound to the following view model.
I have this view @model IEnumerable<ViewModelRound2> ... <snip> ... @{ using (Html.BeginForm(new { round1Ring3Bids
I have a view model with a couple of properties that are not displaying
I am using Asp.net MVC 3 and Json.net library. I have view model class
I have the following view model, used for editing Person objects in an HTML
When I have a View Model setup with an ImportingConstructor that takes a single
I came accross the following situation: I have 2 view models and a single
I have a complex view model which contains a collection of other objects which
I have the following view model: public class MyViewModel { public bool SingleFamily {

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.