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

  • Home
  • SEARCH
  • 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 8523415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:24:13+00:00 2026-06-11T07:24:13+00:00

Cant figure out why I don’t receive object in controller. I followed example from

  • 0

Cant figure out why I don’t receive object in controller. I followed example from here: http://www.aspnetmvcninja.com/views/asp-net-mvc-select-list-example

Mine looks like this:
CONTROLLER

private List<webpages_Roles> GetOptions()
    {
        List<webpages_Roles> categories = new List<webpages_Roles>();
        foreach (Models.webpages_Roles databaseRole in Securitydb.webpages_Roles.ToArray())
        {
            categories.Add(new webpages_Roles() { RoleName = databaseRole.RoleName, RoleId = databaseRole.RoleId });
        }        

        return categories;
    }

    public ActionResult Users2()
    {
        UserProfile user = new UserProfile();
        user.UserId = 24;
        user.UserName = "Alan";
        user.webpages_Roles.Add(new webpages_Roles { RoleName = "Administrator", RoleId = 1 });
        ViewBag.Categories = GetOptions();
        return View(user);
    }

    [HttpPost]
    public ActionResult Users2(UserProfile user)
    {
        ViewBag.Categories = GetOptions();
        return View(user);
    }

MODEL

public partial class webpages_Roles
{
    public webpages_Roles()
    {
        this.UserProfiles = new HashSet<UserProfile>();
    }

    public int RoleId { get; set; }
    public string RoleName { get; set; }

    public virtual ICollection<UserProfile> UserProfiles { get; set; }
}
public partial class UserProfile
{
    public UserProfile()
    {
        this.webpages_Roles = new HashSet<webpages_Roles>();
    }

    public int UserId { get; set; }
    public string UserName { get; set; }

    public virtual ICollection<webpages_Roles> webpages_Roles { get; set; }
}

VIEW

@model Models.UserProfile

@{
    ViewBag.Title = "Users2";
}
<div>
<h1>Select Values</h1>
<ul>
@foreach (Models.webpages_Roles c in Model.webpages_Roles)
   {
   <li> @c.RoleId </li>
 }
</ul>
@Html.DisplayForModel()

@Html.HiddenFor(x => x.webpages_Roles)

<h1>Change Values</h1>
@using (Html.BeginForm())
   {
   @Html.ListBoxFor(x => x.webpages_Roles, new MultiSelectList(ViewBag.Categories, "RoleId", "RoleName", Model.webpages_Roles));
   <br />
   <input type="submit" value="Submit" />
 }
    </div>

After selecting changes in the list and pressing submit button

public ActionResult Users2(UserProfile user)

user is empty. Tried using hidden field for each property of UserProfile but no change in behaviour.

  • 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-11T07:24:14+00:00Added an answer on June 11, 2026 at 7:24 am

    In your view you need to wrap form around all the properties from the model. Cause when you submit the form, it only gets ListBox values and that does not match to your UserProfile object. So do something like that:

    @model GEMPlus.Models.UserProfile
    
    @{
        ViewBag.Title = "Users2";
    }
    <div>
        <h1>Select Values</h1>
        @using (Html.BeginForm())
        {
            <ul>
                @foreach (GEMPlus.Models.webpages_Roles c in Model.webpages_Roles)
                {
                    <li> @c.RoleId </li>
                }
            </ul>
            @Html.DisplayForModel()
    
            @Html.HiddenFor(x => x.webpages_Roles)
                @Html.HiddenFor(x => x.RoleId)
    
            <h1>Change Values</h1>
            @Html.ListBoxFor(x => x.webpages_Roles, new MultiSelectList(ViewBag.Categories, "RoleId", "RoleName", Model.webpages_Roles));
            <br />
            <input type="submit" value="Submit" />
        }
    </div>
    

    Make sure at least HiddenFor elements are inside of the form. DisplayFor would not make much difference. Also do include RoleId as a hidden in the form.

    I could not be correct about what exact elements you need to put inside your form, but your problems lies in that area – submitted form does not have enough information to map it to an object.

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

Sidebar

Related Questions

Just cant figure it out. I have a large object stored as a JSON
Can't figure out what's wrong, I don't seem to be getting anything from fread.
I can't figure out why rails has_one :through associations don't accept assignments. Here is
I cant figure out how to get lua to do any common timing tricks,
I want to do something but cant figure out how to do this (i
Im having a odd Problem with the TTS engine and I cant figure out
Hi I keep getting a syntax error but I cant figure out why. My
I am getting segmentation fault in this code but i cant figure out why.
This seems in my head like it should work but I cant figure out
Maybe I missed something, but I cant figure out why Visual Studio 2008 isn't

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.