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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:03:05+00:00 2026-05-30T10:03:05+00:00

I have the following controller and view. It is listing the values correctly using

  • 0

I have the following controller and view. It is listing the values correctly using GET. When I click on the button it causes a POST. However the value received in the controller is NULL. How do we correct it?

HIGHLIGHTED CODE

    [HttpPost]
    public ActionResult CastVote(IEnumerable<Program> theProgramList)

GET Image

enter image description here

CODE

 public enum RatingEnum { Poor = 0, Neutral, Good, Excellent };

public class Program
{
    public int ProgramID { get; set; }
    public string ProgramName { get; set; }
    public RatingEnum RatingID { get; set; }
    public string ProgramCategory { get; set; }
}

CONTROLLER

namespace MyProgramRatingApp.Controllers
{
public class ProgramController : Controller
{




    List<Program> programList = new List<Program>()
                          {
                            new Program
                            {
                                ProgramID = 1,ProgramName = "Program1",
                                ProgramCategory = "A"
                            },
                            new Program
                            {
                                ProgramID = 2,ProgramName = "Program2",
                                ProgramCategory = "B"
                            },
                            new Program
                            {
                                ProgramID = 3,ProgramName = "Program3",
                                ProgramCategory = "A"
                            }

                          };




    // GET: /Program/
    public ActionResult CastVote()
    {
        ViewBag.RatingEnum = GetRstingSelectList();
        return View(programList);
    }


    // POST: /StoreManager/Create
    [HttpPost]
    public ActionResult CastVote(IEnumerable<Program> theProgramList)
    {
        if (ModelState.IsValid)
        {
            //Save the book in DB first and then redirectToAction.


            return RedirectToAction("CastVote");
        }

        return View(theProgramList);
    }


    public static SelectList GetRstingSelectList()
    {
        Array values = Enum.GetValues(typeof(RatingEnum));
        List<System.Web.UI.WebControls.ListItem> items = new List<System.Web.UI.WebControls.ListItem>(values.Length);

        foreach (var i in values)
        {
            items.Add(new System.Web.UI.WebControls.ListItem
                                    {
                                        Text = Enum.GetName(typeof(RatingEnum), i),
                                        Value = ((int)i).ToString()
                                    }
                       );
        }

        return new SelectList(items);
    }


    }
}

VIEW

@model IEnumerable<MyProgramRatingApp.Program>

@{
ViewBag.Title = "CastVote";
}

<h2>CastVote</h2>

@using (Html.BeginForm())
{
<table>
    <tr>
        <th style="border:1px solid Teal; background-color:Gray">
            ProgramName
        </th>
        <th style="border:1px solid Teal; background-color:Gray">
            RatingID
        </th>
        <th style="border:1px solid Teal; background-color:Gray">
            ProgramCategory
        </th>
        <th style="border:1px solid Teal; background-color:Gray"></th>
    </tr>

@foreach (var item in Model)
{
    <tr>
        <td style="border:1px solid Teal">
            @Html.DisplayFor(modelItem => item.ProgramName)
        </td>
        <td style="border:1px solid Teal">
            @Html.DisplayFor(modelItem => item.RatingID)
        </td>
        <td style="border:1px solid Teal">
            @Html.DisplayFor(modelItem => item.ProgramCategory)
        </td>
        <td style="border:1px solid Teal">
            @Html.DropDownListFor(model => item.RatingID, (SelectList)ViewBag.RatingEnum, String.Empty)
        </td>

    </tr>
}

</table>

<p>
    <input type="submit" value="Cast Vote" />
</p>

}

READING:

  1. dropdownlist set selected value in MVC3 Razor

  2. ASP.NET MVC 3 – Partial vs Display Template vs Editor Template

  3. IEnumerable property with MVC3 EditorTemplate

  4. ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries

  5. Model Binding To A List


  • 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-30T10:03:07+00:00Added an answer on May 30, 2026 at 10:03 am

    Replace the foreach loop in your view with a call to an editor template:

    @model IEnumerable<MyProgramRatingApp.Program>
    
    @{
        ViewBag.Title = "CastVote";
    }
    
    <h2>CastVote</h2>
    
    @using (Html.BeginForm())
    {
        <table>
            <tr>
                <th style="border:1px solid Teal; background-color:Gray">
                    ProgramName
                </th>
                <th style="border:1px solid Teal; background-color:Gray">
                    RatingID
                </th>
                <th style="border:1px solid Teal; background-color:Gray">
                    ProgramCategory
                </th>
                <th style="border:1px solid Teal; background-color:Gray"></th>
            </tr>
    
            @Html.EditorForModel()
    
        </table>
    
        <p>
            <input type="submit" value="Cast Vote" />
        </p>
    }
    

    and then define the editor template which will be automatically rendered for each element in the model (~/Views/Shared/EditorTemplates/Program.cshtml):

    @model MyProgramRatingApp.Program
    <tr>
        <td style="border:1px solid Teal">
            @Html.EditorFor(x => x.ProgramName)
        </td>
        <td style="border:1px solid Teal">
            @Html.EditorFor(x => x.RatingID)
        </td>
        <td style="border:1px solid Teal">
            @Html.EditorFor(x => x.ProgramCategory)
        </td>
        <td style="border:1px solid Teal">
            @Html.DropDownListFor(
                 x => x.RatingID, 
                 (SelectList)ViewBag.RatingEnum, 
                 String.Empty
            )
        </td>
    </tr>
    

    Notice that I have used @Html.EditorFor in the editor template instead of @Html.DisplayFor in order to generate input fields. If you don’t do that you won’t get any values back in the controller because your form doesn’t contain any input elements. If you don’t want to show input fields you could use hidden inputs:

    @model MyProgramRatingApp.Program
    <tr>
        <td style="border:1px solid Teal">
            @Html.DisplayFor(x => x.ProgramName)
            @Html.HiddenFor(x => x.ProgramName)
        </td>
        <td style="border:1px solid Teal">
            @Html.DisplayFor(x => x.RatingID)
            @Html.HiddenFor(x => x.RatingID)
        </td>
        <td style="border:1px solid Teal">
            @Html.DisplayFor(x => x.ProgramCategory)
            @Html.HiddenFor(x => x.ProgramCategory)
        </td>
        <td style="border:1px solid Teal">
            @Html.DropDownListFor(
                 x => x.RatingID, 
                 (SelectList)ViewBag.RatingEnum, 
                 String.Empty
            )
        </td>
    </tr>
    

    The editor template will generate correct names for the input fields so that the model binder correctly binds the values.

    You may also take a look at the following article to better understand the wire format that is expected for collections.

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

Sidebar

Related Questions

I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following ActionLink in my view <%= Html.ActionLink(LinkText, Action, Controller); %> and
If i have a controller action Create that returns a view with the following
I have the following in my controller: var $cacheAction = array( 'view/' => 432000,
I have the following controller and view. I am trying to learn jquery in
I am making a color picker. I have the following code: View controller.h: @interface
I have the following controller view method for my posts: function view($id = null)
I have the following controller, and I get the error Notice (8): Undefined property:
I have the following code in a view controller that (in all other respects)
I have the following line in my view controller: -(void) retrieveAccounts { accounts =

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.