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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:17:04+00:00 2026-05-26T10:17:04+00:00

All, please clear up my confusion on how the model binding works with IEnumerables

  • 0

All, please clear up my confusion on how the model binding works with IEnumerables and Editor Templates.

I have a view, Approve.cshtml

@model IEnumerable<MvcWebsite.Models.Approve>
<table>
    <tr>
        <th>
            Name
        </th>
    </tr>
    @Html.EditorForModel()
</table>

A model, Approve.cs

public class Approve
{
  public string Name { get;set;}
  public string Role { get; set; }
}

And an editor template

@model MvcWebsite.Models.Approve

@using (Html.BeginForm("Approve", "Registration", FormMethod.Post))
{
<tr>
    <td>
        @Html.HiddenFor(m => m.Name)
        @Html.EditorFor(m => m.Role)
    </td>
    <td>
        <input type="submit" value="Approve" class="submit-button" />            
    </td>
</tr>

}

This is all fine and good. It renders the following output.

        <input name="[0].Name" type="hidden" value="" />
        ....

However, in my Controller, I can’t seem to receive values back for the Model (binding).

[HttpPost]
public ActionResult Approve(Approve approveModel)
{
    .... approveModel has all default values
}

Can someone shed light on what I am doing wrong here? I abbreviated the code, I am using the Editor Template with other EditorFor and HiddenFor fields from my model…

Edited: I basically have a table layout, each with the User’s Name, a textbox where I can enter their role (User or Admin) and then an Approve button which submits to my controller. Hence the reason I want to only return a single Approve object. I can return the entire IEnumerable to my Controller, but if I do that, how can I tell which of the items was the one I clicked the Approve button (submit) for?

EDIT:
So I have modified the code so that I have a single form surrounding my entire View Approve.cshtml

@model IEnumerable<MvcWebsite.Models.Approve>
@using (Html.BeginForm("Approve", "Program", FormMethod.Post))
{
<table>
    <tr>
        <th>
            Name
        </th>
    </tr>
    @Html.EditorForModel()
</table>
}

And then changed the controller to

[HttpPost]
public ActionResult Approve(IEnumerable<Approve> approvals)
{
    // ???????????????????????
}

Now I’m still not clear on how to know which row I clicked Approve for. I know there are other ways to accomplish this task (create a checkbox for approve, and approve anything checked, etc.) However, I need the ability to click a button and only save 1 row back to the database, regardless if the user entered information into the other rows. Is it better practice to wrap my IEnumerable inside of it’s own model (i.e. AllApprovals) and then add helper properties to that parent model (SelectedIndex, etc.)? If that is the approach to take, then how do I set the SelectedIndex after clicking an Approve button? Is that still jquery magic or is there a correct MVC way to accomplish this? Jquery magic seems very hackish to me?

EDIT: Based on Brian’s response, here is my final. Still doesn’t feel quite right, but it works!

View

@model IEnumerable<MvcWebsite.Models.Approve>
<table>
    <tr>
        <th>
            Name
        </th>
    </tr>
    @Html.EditorForModel()
</table>

Editor Template

@using (Html.BeginForm("Approve", "Registration", FormMethod.Post))
{
<tr>
    <td>
        @Html.HiddenFor(m => m.Name)
        @Html.EditorFor(m => m.Role)
    </td>
    <td>
        <input type="submit" value="Approve" class="submit-button" />            
    </td>
</tr>
}

Controller

[HttpPost]
public ActionResult Approve([Bind(Prefix="approval")]Approve approval)    {
    // WORKS!
}
  • 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-26T10:17:05+00:00Added an answer on May 26, 2026 at 10:17 am

    Since you are only changing one at a time, I think the following is easier than trying to figure out at the controller which values changed, or adding a changed property and setting it via javascript.

    Change Approve.cshtml to

    @model IEnumerable<MvcWebsite.Models.Approve> 
    <table> 
        <tr> 
            <th colspan=2> 
                Name 
            </th> 
        </tr> 
    @foreach(var user in Model){
        @using (Html.BeginForm("Approve", "Registration", FormMethod.Post))         { 
        <tr> 
            <td> 
                @Html.EditorFor(m => user) 
            </td> 
            <td> 
                <input type="submit" value="Approve" class="submit-button" />             
            </td> 
        </tr> 
        }
    }
    </table> 
    

    Change the Approve Editor Template to

    @Html.HiddenFor(m => m.Name) 
    @Html.EditorFor(m => m.Role) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear All, please help me since I'm newbie in SQL Server. I have a
Please help me clear this doubt I have in my head once and for
Can you please does android clear/remove all user preference during upgrade? upgrade my apk
I was involved in couple of cloud computing platform recently. First of all please
Please tell me how can I get array of all Those DIV of specific
This is all hypothetical, so please bear with me. Say I'm writing a tool
Hi all (before closing based on the title, please read the bottom parragraph), I'm
please try this link for getting my code. its working in all browsers not
i am very confused please help to clarify these differences! are they all completely
Possible Duplicate: VS2008 Setup Project: Shared (By All Users) Application Data Files? Please can

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.