C# ASP.Net MVC3下拉列表和传递数据

C# ASP.Net MVC3下拉列表和传递数据,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我有这个控制器 public ActionResult Index() { IList<Partner> p = r.ListPartners(); ViewBag.Partners = new SelectList(p.AsEnumerable(), "PartnerID", "Name"); return View(); } // // POST: /

我有这个控制器

    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();
    }
public ActionResult Index()
{        
IList p=r.ListPartners();
ViewBag.Partners=新选择列表(p.AsEnumerable(),“PartnerID”,“Name”);
返回视图();
}
//
//POST:/Epub/
[HttpPost]
公共操作结果索引(IEnumerable fileUpload)
{            
IList p=r.ListPartners();
ViewBag.Partners=新选择列表(p.AsEnumerable(),“PartnerID”,“Name”);
整数计数=0;
对于(int i=0;i0)
{
var fileName=Path.GetFileName(file.fileName);
//需要修改此选项以将文件保存到服务器
var path=path.Combine(Server.MapPath(“/App_Data/uploads”)、Guid.NewGuid()+“-”+文件名);
file.SaveAs(路径);
}
}
}
如果(计数=0)
{
AddModelError(“,”您必须至少上载一个文件!”);
}
返回视图();
}
以及相应的观点

        @{
    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)
}
@{
ViewBag.Title=“Index”;
}
您已进入epub门户!
@Html.Partial(“_下载”)
@模型IEnumerable
@{            
@DropDownList(“PartnerID”,(IEnumerable)ViewBag.Partners)
}
我试图弄清楚如何将所选PartnerID从视图传递回控制器。任何指点都将不胜感激。我看过其他帖子,但似乎找不到一个解决方案,将有助于这一点


提前感谢您抽出时间

您可以创建一个接受FormCollection参数的ActionResult,以便从视图中的控件获取如下值:

在要填充列表的视图中:

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

这与使用ViewBag存储值有何不同?发布表单时,我需要获取从dropdownlist中选择的项目的值,并在发布时重新选择该值,这样用户就不必这样做。ViewBag对象用于将值从控制器传递到视图。我之前写的是如何将值从视图传递到控制器,这是您在文章中提出的问题。您可以使用ViewBag将值传递给视图,然后当用户发布视图时,使用上述技术获取该值,然后当您需要再次显示视图时,再次使用ViewBag传递所选值并在DropDownList中选择适当的项目。这有意义吗?非常感谢你的帮助,我已经弄明白了这一点,并感谢你的意见。如果我有代表的话,我会投你一票。没问题,我们都在这里帮助你不被选上。呵呵;)我很乐意帮忙!
public ActionResult MyActionButton(FormCollection collection)
{
    string value = collection["MyList"];
    return View();
}