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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:34:52+00:00 2026-05-28T23:34:52+00:00

I’ve a form in which I only have to choose between several entities. So

  • 0

I’ve a form in which I only have to choose between several entities.

So here is what I’ve done:

Created a view Model:

    public class EntityChooserModel
    {
        public IEnumerable<Entity> AvailableEntities { get; set; }
        public int SelectedId;
    }

I’ve two actions in the controller, one with [HTTPPost], one without. In both I have a “SessionUserData”, which is session data, bound through data model binding.

        [Authorize]
        public ActionResult EntityChooser(SessionUserData sessionData)
        {
            List<Entity> entities = _userStore.GetRoles(User.Identity.Name).Select(ur => ur.Entity).ToList();


            Entity entity = sessionData.IdCurrentEntity != null ? entities.Where(e => e.Id == sessionData.IdCurrentEntity).First() : entities.First();

            return View(new EntityChooserModel() { SelectedId = entity.Id, AvailableEntities = entities });
        }

.

    [Authorize]
    [HttpPost]
    public ActionResult EntityChooser( EntityChooserModel model, SessionUserData sessionData, String returnUrl)
    {
        if (ModelState.IsValid)
        {
            Entity entity = _userStore.GetRoles(User.Identity.Name).Select(ur => ur.Entity).Where(e => e.Id == model.SelectedId).First();
            sessionData.IdCurrentEntity = entity.Id;
            RedirectToDefaultPage(returnUrl);
        }
        return View(model);
    }

And the strongly typed view:

@model My.Full.Namespace.EntityChooserModel
@{
    ViewBag.Title = "EntityChooser";
    Layout = "~/Views/Shared/_LoginLayout.cshtml";
}
<div id="login-wrapper">
    <div id="title">
        <h1>Login</h1>
    </div>
    <div id="login">
        @using (Html.BeginForm("EntityChooser", "Auth", FormMethod.Post, new { id = "ChooseFormId" }))
        {            
            @Html.ValidationSummary(true, "Loggin error")
            <div>
                <span>Choose an entity:</span>
                @Html.DropDownListFor(x => x.SelectedId, new SelectList(Model.AvailableEntities, "Id", "Name"))
            </div>
            <p class="right-aligned">
                <a href="javascript:document.getElementById('ChooseFormId').submit();" class="adm-button">
                    <span class="next-btn"><span>Select</span></span></a>
            </p>
            <input type="submit" style="display: none" />
        }
    </div>
</div>

The problem is that when I receive the data in the second model, I’ve an empty object. The collection is empty, and the id is set to the default value(0).

What is wrong? I can’t find out what I missed on this one.

It’s the first time I do an mvc form which isn’t ForModel(), and the first time I’ve to use a dropdownlist

  • 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-28T23:34:53+00:00Added an answer on May 28, 2026 at 11:34 pm

    The only input element that your <form> contains is a single drop down list which sends only the selected value to the server when POSTed. This explains why your AvailableEntities collection property is empty.

    As far as why the SelectedId field is empty, it is because you have defined it as a field and not as a property. The default model binder works only with properties. So if you want it to be populated you must define it as a property:

    public int SelectedId { get; set; }
    

    As far as the AvailableEntities property is concerned you could populate it from your repository in the POST action using the selected id.

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.