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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:08:11+00:00 2026-05-26T00:08:11+00:00

I’m running into an issue trying to use @Html.DropDownListFor(). I have a model with

  • 0

I’m running into an issue trying to use @Html.DropDownListFor().

I have a model with a navigation property on it:

public class Thing {
  ...
  public virtual Vendor Vendor { get; set; }
}

In the controller I’m grabbing the vendor list to throw into the ViewBag:

public ActionResult Create() {
  ViewBag.Vendors = Vendor.GetVendors(SessionHelper.CurrentUser.Unit_Id);
  return View();
}

The html item in the view looks like this:

@Html.DropDownListFor(model => model.Vendor, new SelectList(ViewBag.Vendors, "Id", "Name"), "---- Select vendor ----")
@Html.ValidationMessageFor(model => model.Vendor)

The dropdown list is being rendered, and everything seems fine until I submit the form. The HttpPost Create method is returning false on the ModelState.IsValid and throwing a Model Error: The parameter conversion from type ‘System.String’ to type ‘…Models.Vendor’ failed because no type converter can convert between these types.

If I let the page post through, I end up with a server error:
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: items

After searching high and low I haven’t been able to find a reason that the @Html.DropDownListFor() isn’t properly auto-binding a Vendor object to the navigation property.

Any help would be greatly appreciated.

EDIT:
I ended up having to explicitly set the ForeignKey attributes so that I could directly access “Vendor_Id” then I changed the DropDownListFor to point to “Vendor_Id” instead of the navigation property. That seems to work.

  • 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-26T00:08:11+00:00Added an answer on May 26, 2026 at 12:08 am

    I have found that the best way to do this is as follows. Change the controller to create the SelectListItems.

       public ActionResult Create() {
           ViewBag.Vendors = Vendor.GetVendors(SessionHelper.CurrentUser.Unit_Id)
                                   .Select(option => new SelectListItem
                                   {
                                      Text = (option == null ? "None" : option.Name),
                                      Value = option.Id.ToString()
                                   });
           return View();
        }
    

    Then modify the view as follows:

    @Html.DropDownListFor(model => model.Vendor, (IEnumerable<SelectListItem>)ViewBag.Vendors,    "---- Select vendor ----")
    @Html.ValidationMessageFor(model => model.Vendor)
    

    You have to cast the ViewBag.Vendors as (IEnumerable).

    This keeps the views nice and neat. You could also move the code that gets the SelectListItems to your repo and put it in a method called something like GetVendorsList().

    public IEnumerable<SelectListItem> GetVendorsList(int unitId){
        return Vendor.GetVendors(unitId)
                                   .Select(option => new SelectListItem
                                   {
                                      Text = (option == null ? "None" : option.Name),
                                      Value = option.Id.ToString()
                                   });
    }
    

    This would separate concerns nicely and keep your controller tidy.

    Good luck

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have thousands of HTML files to process using Groovy/Java and I need to
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.