2

I have this Controller

    public ActionResult Index()
    {        
        IList<Partner> p = r.ListPartners();            
        ViewBag.Partners = new SelectList(p.AsEnumerable(), "PartnerID", "Name");
        return View();
    }

    //
    // POST: /Epub/
    [HttpPost]
    public ActionResult Index(IEnumerable<HttpPostedFileBase> fileUpload)
    {            
        IList<Partner> p = r.ListPartners();
        ViewBag.Partners = new SelectList(p.AsEnumerable(), "PartnerID", "Name");          
        int count = 0;
        for (int i = 0; i < fileUpload.Count(); i++)
        {
            if (fileUpload.ElementAt(i) != null)
            {
                count++;
                var file = fileUpload.ElementAt(i);
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    // need to modify this for saving the files to the server
                    var path = Path.Combine(Server.MapPath("/App_Data/uploads"), Guid.NewGuid() + "-" + fileName);
                    file.SaveAs(path);
                }
            }
        }
        if (count == 0)
        {
            ModelState.AddModelError("", "You must upload at least one file!");
        }
        return View();
    }

And the corresponding view

        @{
    ViewBag.Title = "Index";
}

<h2>You are in the epub portal!</h2>
@Html.Partial("_Download")


@model IEnumerable<EpubsLibrary.Models.Partner>
@{            
    @Html.DropDownList("PartnerID", (IEnumerable<SelectListItem>)ViewBag.Partners)
}

I am trying to figure out how to pass the selected PartnerID from the view back to the controller. Any pointers would be appreciated. I have looked at other posts but cannot seem to locate a solution that will help with this.

Thanks in advance for your time.

1 Answer 1

1

You can create an ActionResult accepting a FormCollection parameter in order to get the values from the controls in you view like this:

In the View to Populate the List:

<% using (Html.BeginForm("MyActionButton", "Home")) { %>
   <%= Html.DropDownList("MyList",(SelectList)ViewData["testItems"], "None") %>
   <input type="submit" value="send" />
<% } %>

In the Server side to get the value:

public ActionResult MyActionButton(FormCollection collection)
{
    string value = collection["MyList"];
    return View();
}
Sign up to request clarification or add additional context in comments.

4 Comments

How is this different from using ViewBag to store values? When I post the form I need to get the value of the item selected from the dropdownlist and reselect that on post so the user does not have to.
Well, the ViewBag object is used to pass values from the Controller to the View. What I wrote before is how you can pass values from the View to the Controller which is what you question is in the post. What you can do is use the ViewBag to pass the value to the view, then when the user post the view, get that value using the technique described above, and then when you need to display again the view, use again the ViewBag to pass the selected value and select the appropriate item in your DropDownList. Does that make sense?
Thank you very much for your help I have figured this out and appreciate your input. I would upvote you if I had the rep.
No problem about that, we are all here to help not to get voted hehe ;) I'm glad to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.