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

标签 c# asp.net-mvc-3

我有这个 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();
    }

以及相应的 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 从 View 传递回 Controller 。任何指针将不胜感激。我看过其他帖子,但似乎无法找到有助于解决此问题的解决方案。

提前感谢您的宝贵时间。

最佳答案

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

在 View 中填充列表:

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

在Server端获取值:

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

关于c# - ASP.Net MVC3 下拉列表和传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6833257/

相关文章:

validation - 使用其他本地时更新小数

c# - MVC3 不发布整个模型

c# - SignalR 客户端默认回退传输

c# - 将大文件发布到 REST Endpoint c# .net core

c# - 使用对象类型作为参数

c# - 检测对象是否为 ValueTuple

c# - Lottie.Forms - 从 EmbeddedResources 加载

asp.net-mvc - 如何在 mvc 中的 Homecontroller 内根据条件更改提交按钮文本?

asp.net-mvc-3 - 在MVC3非强制性远程验证上强制重新验证

asp.net-mvc-3 - 使用 Azure 缓存(.NET MVC3 应用程序)时,为什么无法组合 [Authorize] 和 [OutputCache] 属性?