C# 获取 DropDownList 的选定值。ASP.NET MVC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15881575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:17:18  来源:igfitidea点击:

Get the selected value of a DropDownList. Asp.NET MVC

c#.netasp.net-mvcrazorasp.net-mvc-4

提问by Azzedine Hassaini

I'm trying to populate a DropDownList and to get the selected value when I submit the form:

我正在尝试填充 DropDownList 并在提交表单时获取所选值:

Here is my model :

这是我的模型:

public class Book
{
    public Book()
    {
        this.Clients = new List<Client>();
    }

    public int Id { get; set; }
    public string JId { get; set; }
    public string Name { get; set; }
    public string CompanyId { get; set; }
    public virtual Company Company { get; set; }
    public virtual ICollection<Client> Clients { get; set; }
}

My Controllers :

我的控制器:

    [Authorize]
    public ActionResult Action()
    {
        var books = GetBooks();
        ViewBag.Books = new SelectList(books);
        return View();
    }

    [Authorize]
    [HttpPost]
    public ActionResult Action(Book book)
    {
        if (ValidateFields()
        {
            var data = GetDatasAboutBookSelected(book);
            ViewBag.Data = data;
            return View();
        }
        return View();
    }

My Form :

我的表格:

@using (Html.BeginForm("Journaux","Company"))
{
<table>
    <tr>
        <td>
            @Html.DropDownList("book", (SelectList)ViewBag.Books)
        </td>
    </tr>
    <tr>
        <td>
            <input type="submit" value="Search">
        </td>
    </tr>
</table>
}

When I click, the parameter 'book' in the Action is always null. What am I doing wrong?

当我单击时,Action 中的参数“book”始终为空。我究竟做错了什么?

采纳答案by Darin Dimitrov

In HTML a dropdown box sends only simple scalar values. In your case that would be the id of the selected book:

在 HTML 中,下拉框仅发送简单的标量值。在您的情况下,这将是所选书籍的 ID:

@Html.DropDownList("selectedBookId", (SelectList)ViewBag.Books)

and then adapt your controller action so that you will retrieve the book from the id that gets passed to your controller action:

然后调整您的控制器操作,以便您从传递给控制器​​操作的 id 中检索书:

[Authorize]
[HttpPost]
public ActionResult Action(string selectedBookId)
{
    if (ValidateFields()
    {
        Book book = FetchYourBookFromTheId(selectedBookId);
        var data = GetDatasAboutBookSelected(book);
        ViewBag.Data = data;
        return View();
    }
    return View();
}

回答by mesut

You can use DropDownListFor as below, It so simpler

你可以使用 DropDownListFor 如下,它更简单

@Html.DropDownListFor(m => m.Id, new SelectList(Model.Books,"Id","Name","1"))

(You need a strongly typed view for this -- View bag is not suitable for large lists)

(为此您需要一个强类型视图——视图包不适合大列表)

   public ActionResult Action(Book model)
   {
        if (ValidateFields()
        {
            var Id = model.Id;
        ...        

I think this is simpler to use.

我认为这更易于使用。