-1

I am trying to load data into my page through a parameter, but the parameter is not being passed. Here is the part of my view that is passing a value:

@using (Html.BeginForm()){
    <text>Team </text>@Html.DropDownList("Tm", new SelectList(ViewBag.Teams))
    <input type="submit" value="Search" />
}`

The "Tm" should be the value that is getting passed once a value is selected and the submit button is clicked. It should by passed to the following function in the controller

public ActionResult Index(int? Tm)
    {
        if (Tm == 0)
        {
            Tm = 1;
        }
        string connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\Webs\MvcFFL\MvcFFL\App_Data\aspnet-MvcFFL-20140517174741.mdf;Integrated Security=True";
}

unfortunately the value of Tm comes in as null. If I removed the ? from int in the controller then it throws an error. So what can i do to make sure the value is passed so my controller can send the data back to the page correctly?

**UPDATE** I am posting the thing that is returning to the page:

var mdl = from r in db.Teamplayers
                    where r.teamid == f
                    select r;

        return View(mdl);

f is an integer in this case

UPDATE #2 Here is my ViewBag.Teams, that the selectlist gets the info from:

ViewBag.Teams = (from r in db.FTeams
                         where r.UserID == curID
                         select r.FTeam).Distinct();
user3240928
  • 525
  • 2
  • 4
  • 16

1 Answers1

0

Try to set the value field and text field explicitly for the dropdown:

 @Html.DropDownList("TM",new SelectList(ViewBag.Teams,"Value","Text"))

Otherwise, You have to pass the variable holding the value of selected item in dropdown list.

Either you need a hidden field in the form and you have to assign the value of selected item to that hidden field via JavaScript or

mark the property in Model as holder of the value :

  @Html.DropDownListFor(m => m.TM, 
            new SelectList(ViewBag.Teams))
Ash
  • 2,575
  • 2
  • 19
  • 27