简体   繁体   中英

ASP.NET MVC DropDownListFor returns null on post back

I want to ask a very simple question that is I'm creating a simple MVC app to test drop down functionality, there are 2 action methods in the controller one is for GET and other is for POST. I want to get the selected value from the drop down list using HTML form helper post back. Here are my M, V and C;

Model:

public class Programming
      {
           public int selectedId { get; set; }

            public List<SelectListItem> languanges;
      }

Controller:

public class HomeController : Controller
 {
   private Programming programming;
   private List<SelectListItem> list;

   public HomeController()
     {
        programming = new Programming();
        programming.languanges = new List<SelectListItem>();


        list = new List<SelectListItem>() {
        new SelectListItem(){ Value="1", Text="ActionScript"},
        new SelectListItem(){ Value="2", Text="AppleScript"},
        new SelectListItem(){ Value="3", Text="Asp"},
        new SelectListItem(){ Value="4", Text="BASIC"},
        new SelectListItem(){ Value="5", Text="C"},
        new SelectListItem(){ Value="6", Text="C++"},
        new SelectListItem(){ Value="7", Text="Clojure"},
        new SelectListItem(){ Value="8", Text="COBOL"},
        new SelectListItem(){ Value="9", Text="ColdFusion"},
        new SelectListItem(){ Value="10", Text="Erlang"},
        new SelectListItem(){ Value="11", Text="Fortran"},
        new SelectListItem(){ Value="12", Text="Groovy"},
        new SelectListItem(){ Value="13", Text="Haskell"}, 
        new SelectListItem(){ Value="14", Text="instinctcoder.com"},
        new SelectListItem(){ Value="15", Text="Java"},
        new SelectListItem(){ Value="16", Text="JavaScript"},
        new SelectListItem(){ Value="17", Text="Lisp"},
        new SelectListItem(){ Value="18", Text="Perl"},
        new SelectListItem(){ Value="19", Text="PHP"},
        new SelectListItem(){ Value="20", Text="Python"},
        new SelectListItem(){ Value="21", Text="Ruby"},
        new SelectListItem(){ Value="22", Text="Scala"},
        new SelectListItem(){ Value="23", Text="Scheme"},
        };
      }
        // GET: Home
        public ActionResult Index()
        {
            programming.languanges = list;

            return View(programming);
        }

        [HttpPost]
        public ActionResult Index(Programming programming)`enter code here`
        {
            return View();
        }

}

View:

        @model BSTNTest.Models.Programming

        @{
            ViewBag.Title = "Index";
          }

        @using (Html.BeginForm("Index", "Home", FormMethod.Post))
        {
            @Html.DropDownListFor(c => c.selectedId, Model.languanges)
            <button type="submit" class="btn btn-primary">Submit</button>
        }

If I put a breakpoint at the post action I get the languages null! Why it is happening?

And finally end up with a null exception.

Thanks

You have to assing List not in the Index method but where it is created, in constructor:

   public HomeController()
     {
        programming = new Programming();
        programming.languanges = new List<SelectListItem>();


        list = new List<SelectListItem>() {
        new SelectListItem(){ Value="1", Text="ActionScript"},
        new SelectListItem(){ Value="2", Text="AppleScript"},
        new SelectListItem(){ Value="3", Text="Asp"},
        new SelectListItem(){ Value="4", Text="BASIC"},
        new SelectListItem(){ Value="5", Text="C"},
        new SelectListItem(){ Value="6", Text="C++"},
        new SelectListItem(){ Value="7", Text="Clojure"},
        new SelectListItem(){ Value="8", Text="COBOL"},
        new SelectListItem(){ Value="9", Text="ColdFusion"},
        new SelectListItem(){ Value="10", Text="Erlang"},
        new SelectListItem(){ Value="11", Text="Fortran"},
        new SelectListItem(){ Value="12", Text="Groovy"},
        new SelectListItem(){ Value="13", Text="Haskell"}, 
        new SelectListItem(){ Value="14", Text="instinctcoder.com"},
        new SelectListItem(){ Value="15", Text="Java"},
        new SelectListItem(){ Value="16", Text="JavaScript"},
        new SelectListItem(){ Value="17", Text="Lisp"},
        new SelectListItem(){ Value="18", Text="Perl"},
        new SelectListItem(){ Value="19", Text="PHP"},
        new SelectListItem(){ Value="20", Text="Python"},
        new SelectListItem(){ Value="21", Text="Ruby"},
        new SelectListItem(){ Value="22", Text="Scala"},
        new SelectListItem(){ Value="23", Text="Scheme"},
        };
        programming.languanges = list;
      }

And be carefull: you have in post method parameter with the same name as your global variable, so you are hiding it. In Post method you should return view with model to have the list there.

[HttpPost]
public ActionResult Index(Programming par)
{
    return View(programming);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM