ASP.Net MVC3 DROPDOWNLIST和传递数据 [英] ASP.Net MVC3 Dropdownlist and passing data

查看:260
本文介绍了ASP.Net MVC3 DROPDOWNLIST和传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个控制器

    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();
    }

和相应的视图

        @{
    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)
}

我试图找出如何选择PARTNERID从视图到控制器通过。任何指针将AP preciated。我已经看过其他职位,但似乎无法找到一个解决方案,将帮助这一点。

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.

推荐答案

您可以在您查看这样创建一个ActionResult接受的FormCollection参数,以便从控件获取值:

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

在View来填充列表:

In the View to Populate the List:

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

在服务器端来获取值:

public ActionResult MyActionButton(FormCollection collection)
{
    string value = collection["MyList"];
    return View();
}

这篇关于ASP.Net MVC3 DROPDOWNLIST和传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆