0

My problem is that I am trying to setup a drop down list and when a new item is selected it will update a table of information that I have but every time HttpPost function gets called the parameter is always null. Here is index function of my home controller:

public ActionResult Index()
{
   Project[] projects = db.Projects.ToArray();
   List<SelectListItem> dropList = new List<SelectListItem>();
   BurndownSprintTable[] TableToShow = db.BurndownSprintTables.ToArray();

   for (int index = 0; index < projects.Length; ++index)
        {
            dropList.Add(new SelectListItem { Text = projects[index].Name, Value = projects[index].Id.ToString(), Selected = projects[index].Selected });
            if (projects[index].Selected == true)
            {
                int ProjectId = projects[index].Id;
                TableToShow = db.BurndownSprintTables.Where(x => x.ProjectId == ProjectId).ToArray();
            }
        }
  ViewBag.Projects = dropList;
  return View(TableToShow);
}

TableToShow is used to show sprint information per project.

Here is the post function:

[HttpPost]
    public ActionResult ProjectUpdate(string strProjectId)
    {
      return View("Index");
    }

I realize this post function will cause an error which I believe I can fix on my own but I need strProjectId to not be null.

Here is the html:

@model IEnumerable<TableauConfigWebService.Models.BurndownSprintTable>
@using (Html.BeginForm("ProjectUpdate", "Home", FormMethod.Post))
{
<h5>@Html.DropDownList("Id", (IEnumerable<SelectListItem>)ViewBag.Projects)

<input type="submit" value="Update" /></h5>
}

There is more to it but the rest of the html is setting up a table for the sprint information which works fine.

I have looked around and found a bunch of posts on this but none of them seem to help. I am new to mvc so I know its probably something simple but I cant figure it out.

Thank you for any help.

4

3 回答 3

1

Change to this

         [HttpPost]
           public ActionResult ProjectUpdate(string Id)
                {
                    return View("Index");
                }

Or change the control Name to

      @Html.DropDownList("strProjectId", (IEnumerable<SelectListItem>)ViewBag.Projects)
于 2013-10-17T11:56:48.857 回答
0

Try this simple way , and check it ,

Must be same names are dropdown and model property .

But you can try this simple way

[HttpPost]
    public ActionResult ProjectUpdate()
    {
     **string strProjectId=Request.Form["strProjectId"].ToString();**//Or use Request["strProjectId"].ToString();
      return View("Index");
    }

@Html.DropDownList("**strProjectId**", (IEnumerable<SelectListItem>)ViewBag.Projects)

or

Simply, you can use FormCollection like,

[HttpPost]
        public ActionResult ProjectUpdate(FormCollection collection)
        {
         **string strProjectId=collection["strProjectId"].ToString();**
          return View("Index");
        }

    @Html.DropDownList("**strProjectId**", (IEnumerable<SelectListItem>)ViewBag.Projects)

And check my code with using break points !

于 2013-10-17T12:00:27.197 回答
0

Use the same parametre name at both the place in view and controller.

either change in controller.

public ActionResult ProjectUpdate(string Id)
    {
      return View("Index");
    }

or else change in view dropdown code to this.

@Html.DropDownList("strProjectId", (IEnumerable<SelectListItem>)ViewBag.Projects)

hope this helps...

于 2013-10-17T12:07:31.187 回答