Home > Enterprise >  Is it possible to send multiple values from one select option in form?
Is it possible to send multiple values from one select option in form?

Time:10-11

If you select option 2 for example, I want to send both the value 2 (for the amount of books to be added) but also the Book_ID for the chosen book.

So in my method that retrieves the form I expect to get both an integer value of 2 and also an integer value for my Book_ID.

I was hoping you could do something like <option [email protected]_ID value="1" >1</option> but that obviously didn't seem to work.

Below is a code snippet from my current View.

@foreach (Lab2.Models.ShoppingCartDetail ShoppingCartItem in Model.ShoppingcartList)
{
    <tr>
        
        <td>@ShoppingCartItem.Title</td>
        <td>@ShoppingCartItem.Author</td>
        <td>@ShoppingCartItem.Price :-</td>
        <td>@ShoppingCartItem.NumberOfBooks</td>
        <td>
            <form action="UpdateNumberOfBooks" method="POST">
                <div >
                    <select  id="NumberOfBooks" name="NumberOfBooks" onchange="this.form.submit()">
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                    </select>
                </div>
                
            </form>
        </td>

        
    </tr>
}

If it's possible, how should my method look that retrieves this information?

CodePudding user response:

Just add that value as an <input> to your form. For example:

<input type="hidden" name="Book_ID" value="@ShoppingCartItem.Book_ID" />

This would include the Book_ID as a separate value in the same <form>.

how should my method look that retrieves this information?

Presumably you have a method which receives something like this, no?:

public IActionResult UpdateNumberOfBooks(int numberOfBooks)

If that's the case, you'd just include this second value as well:

public IActionResult UpdateNumberOfBooks(int book_ID, int numberOfBooks)

Or if NumberOfBooks is included as part of a model, you'd add Book_ID to that model. Basically, however you currently receive the one value you have now, you'd add the new value alongside it.

  • Related