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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:41:14+00:00 2026-05-31T18:41:14+00:00

I am making a View that has a DropDown. The view is supplied with

  • 0

I am making a View that has a DropDown. The view is supplied with a CategoryModel that looks like this:

public class CategoryModel
{
    [Required]
    [Display(Name = "Categories")]
    public List<Category> Categories { get; set; }

    [Required]
    [GreaterThan(ExceedValue = 0, ErrorMessage = "Please select a category.")]
    [Display(Name = "SelectedCategoryId")]
    public int SelectedCategoryId { get; set; }
}

The List of Categories is used within the view to populate the DropDown by first taking the categories and put them in a SelectList, like this:

@model RatingMVC3.Models.CategoryModel

@{
Layout = "~/Views/Shared/_MainLayout.cshtml";
ViewBag.Title = "Upload";
}

<h2>Upload</h2>

@using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type="submit" value="OK" />
    @Html.DropDownListFor(m => m.SelectedCategoryId, new SelectList(Model.Categories, "Id", "Description"), "-- Select Category --");
    @Html.ValidationMessageFor(m => m.SelectedCategoryId);
    @Html.HiddenFor(m => m.Categories);
}

When this form is submitted, and the model is returned to the Controller, I can see that the model comes back into the Controller, but it contains an empty List instead of the List of Categories that was there in the View. (The SelectedCategoryId is there as expected). Here is the ActionResult method in the Controller:

    [HttpPost]
    [Authorize]
    public ActionResult Upload(HttpPostedFileBase file, CategoryModel model)
    {
        if (ModelState.IsValid)
        {
            if (file != null && file.ContentLength > 0)
            {
                var FileExtension = Path.GetExtension(file.FileName);
                string Path1 = null;
                string FileName = null;
                do
                {
                    var randomName = Path.GetRandomFileName();
                    FileName = Path.ChangeExtension(randomName, FileExtension);
                    Path1 = Path.Combine(Server.MapPath("~/Images"), FileName);
                } while (System.IO.File.Exists(Path1));
                file.SaveAs(Path1);
                if (UploadService.SaveImage(FileName, System.Web.HttpContext.Current.User.Identity.Name, model.SelectedCategoryId))
                {
                    return RedirectToAction("Uploaded", "Upload");
                }
            }
            return RedirectToAction("Index", "Home");
        }

        return View(model);
    }

The empty list is a problem for me, because as you can see, if the ModelState is not valid, the view will be returned with the same model again, and needs to be populated with categories.
I hope someone can answer this 😉 I will gladly specify more information if needed, Thanks in advance

  • 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-31T18:41:16+00:00Added an answer on May 31, 2026 at 6:41 pm

    During a POST operation, the entire select list (drop down) is not returned to the controller/form action, only the selected value from that list. If you have to re-render the view because of bad validation, you can either a) Do an AJAX submit instead of a full POST for validation reasons or b) recreate your list and send it back with the View on Error. Option b) can be implemented as such:

    [HttpPost]
        [Authorize]
        public ActionResult Upload(HttpPostedFileBase file, CategoryModel model)
        {
            if (ModelState.IsValid)
            {
                if (file != null && file.ContentLength > 0)
                {
                    var FileExtension = Path.GetExtension(file.FileName);
                    string Path1 = null;
                    string FileName = null;
                    do
                    {
                        var randomName = Path.GetRandomFileName();
                        FileName = Path.ChangeExtension(randomName, FileExtension);
                        Path1 = Path.Combine(Server.MapPath("~/Images"), FileName);
                    } while (System.IO.File.Exists(Path1));
                    file.SaveAs(Path1);
                    if (UploadService.SaveImage(FileName, System.Web.HttpContext.Current.User.Identity.Name, model.SelectedCategoryId))
                    {
                        return RedirectToAction("Uploaded", "Upload");
                    }
                }
                return RedirectToAction("Index", "Home");
            }
            //I don't know how you fetch this list, but just do it again before returning
            //the view
            model.Categories = GetMyCategories();
            return View(model);
        }
    

    I guess if you wanted, you could store the list of items as a hidden input on your page, but that just looks ugly, adds to the amount of data being sent to and from the web page and gosh darn it, just sounds a heck of a lot like ViewState (shrudders).

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

Sidebar

Related Questions

I'm making Android app that has this database in it: public void onCreate(SQLiteDatabase db)
I'm making an application for the iPad. I have a view controll that has
I'm in a view that has a UIPickerView. Here is where I'm making my
I'm making an iOS application that has an interface in the portrait view. However,
I would like to make a class that has different text to say to
I am making a custom view that has 2 elemnts: a background that consists
I'm making an app that has a view with UITableView with UISearchDisplayController . Data
I'm making a RoR app that has 3 resources, that are nested in this
So i'm making a custom View that displays some graphs (plots), and in some
I'm making a WPF application that is comprised of Screens (Presenter + View). I

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.