0

Hi guys I have a dropdownlist and a submit button in my view. I want the user to be able to select an item in the dropdownlist that calls an action method in the controller that gets data from the database.

I also have a button and I want the user to be able to select a checkbox in a grid and click the submit button to pass the value of the checkbox to an action method in the controller.

The problem is when I select an item from the dropdownlist it calls the action method for the submit button "DiscontinueProduct" and not the action method for the dropdownlist ("GetProductByID"), can someone please tell me what I'm doing wrong? Here is a my code.

Thanks in advance.

=============

view

<div>
@Using Html.BeginForm("GetProductByID", "Product")

    @Html.DropDownList("CategoryID", DirectCast(ViewData("Categories"), SelectList), " -- Choose One -- ", New With {Key .onchange = "$('form').submit();"})
End Using
</div>

@Using Html.BeginForm("DiscontinueProduct", "Product")
    @<text>
<table>
    <tr>
        <th></th>
        <th>ProductName</th>
        <th>SupplierID</th>
        <th>CategoryID</th>
        <th>Discontinued</th>
    </tr>
@For Each item In Model
    @<tr>
        <td>
            @Html.ActionLink("Edit", "Edit", New With {.id = item.ProductID}) |
            @Html.ActionLink("Details", "Details", New With {.id = item.ProductID}) |
            @Html.ActionLink("Delete", "Delete", New With {.id = item.ProductID})
        </td>
        <td>@item.ProductName</td>
        <td>@item.SupplierID</td>
        <td>@item.CategoryID
            <input type="checkbox" name="task" id="isTaskSelected" value=" @item.CategoryID.ToString() " />
        </td>
        <td>@item.Discontinued</td>
    </tr>
Next

</table>
<div id="btncomplete" style="display: none">
    <input type="submit" value="Discontinue" />         
</div>
</text>
End Using

=====================

Controller

Function GetProductByID(ByVal id As Integer) As ActionResult

Dim cquery2 = From product In db.Products
                         Where product.CategoryID = id
            viewmodel.ProductList = cquery2.ToList()
            Return PartialView("Products", viewmodel.ProductList)
Return PartialView("Products", viewmodel.ProductList)

 End Function


    <HttpPost()> _
    Function DiscontinueProduct(ByVal collection As FormCollection) As ActionResult
        Try
            ' Code to update product field as discontinue.


            Return RedirectToAction("Index")
        Catch
            Return View()
        End Try
    End Function
hunter
  • 62,308
  • 19
  • 113
  • 113
Tony
  • 1
  • 1
  • 2

1 Answers1

0

It seems that you have to prevent the SUBMIT action for your drop-down list. Have a look here How to prevent buttons from submitting forms

Community
  • 1
  • 1
NoWar
  • 36,338
  • 80
  • 323
  • 498