获取下拉列表的选定值。Asp。NET MVC

时间:2022-11-15 04:02:20

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

我试图填充下拉列表,并在提交表单时获取所选值:

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?

当我单击时,动作中的参数“book”总是null。我做错了什么?

2 个解决方案

#1


17  

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

#2


1  

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.

我觉得这个用起来比较简单。

#1


17  

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

#2


1  

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.

我觉得这个用起来比较简单。