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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:18:58+00:00 2026-06-18T03:18:58+00:00

For example: Model public class Person { public int ID { get; set; }

  • 0

For example:

Model

public class Person
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string  LastName { get; set; }
}

Editor Template for Person (PersonEditor.cshtml):

@model MvcApplication1.Models.Person

@Html.HiddenFor(x=>x.ID)
<label>First Name</label>    
@Html.TextBoxFor(x=>x.FirstName)
<label>Last Name</label>    
@Html.TextBoxFor(x=>x.LastName)
<br />

On my main page, I want to be able to do the following:

@model IList<MvcApplication1.Models.Person>

@using (Html.BeginForm())
{       
    @Html.EditorFor(x=>x,"PersonEditor")    
}

And have all the elements in the form, generate the proper names automatically; instead of having to loop through the collection as I am doing now:

@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Count; i++)
    {
        @Html.EditorFor(x=>Model[i],"PersonEditor")    
    }   
}

The form elements must contain the following format:

<input name="[0].ID" type="text" value="Some ID" />
<input name="[0].FirstName" type="text" value="Some value" />
<input name="[1].ID" type="text" value="Some x" />
<input name="[1].FirstName" type="text" value="Some y" />

And so on…

Because in my controller, I expect to receive an IList<Person> when the form posts pack.

Can I eliminate that for loop completely?

EDIT

Right now, when I simply do @Html.EditorFor(x=>x) (without the loop, in other words), I get this exception:

The model item passed into the dictionary is of type
‘MvcApplication1.Models.Person[]’, but this dictionary requires a
model item of type ‘MvcApplication1.Models.Person’.

  • 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-18T03:18:59+00:00Added an answer on June 18, 2026 at 3:18 am

    You should be able to use the same template for both IEnumerable<T> and T. The Templating is smart enough to enumerate the IEnumerable, but you will need to rename the editor template to match the type name. Then you should be able to use

    @model IList<MvcApplication1.Models.Person>
    
    @using (Html.BeginForm())
    {       
        @Html.EditorForModel()    
    }
    

    Unfortunately, it looks like a template named anything other than the type name will throw an exception

    The model item passed into the dictionary is of type
    ‘MvcApplication1.Models.Person[]’, but this dictionary requires a
    model item of type ‘MvcApplication1.Models.Person’

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

Sidebar

Related Questions

Take this example model: public class Location { public string Id { get; set;
First, a simple example to describe my problem. Model public class User { public
the best way to explain is with example so: this is the model public
I've written a complex search form in Django. Here is my example model: class
I have a model: class Example(models.Model): unique_hash = models.CharField(max_length=32,unique=True) content = models.FileField(upload_to='source',blank=True,verbose_name=HTML Content File)
I have a model defined as below: class Example(models.Model): user = models.ForeignKey(User, null=True) other
Example models: class Parent(models.Model): name = models.CharField() def __unicode__(self): return self.name class Child(models.Model): parent
For example. class One(models.Model): text=models.CharField(max_length=100) class Two(models.Model): test = models.Integer() many = models.ManyToManyField(One, blank=True)
I don't know what this is called but here goes. public class Person {
The following code illustrates my problem: public class Person : (INotifyPropertyChanged) // INPC only

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.