mvc3 html.Dropdownlist()和html.beginform()

时间:2011-02-14 14:44:02

标签: asp.net asp.net-mvc-3

大家好,我的视图中有一个下拉列表和一个提交按钮。 我希望用户能够在下拉列表中选择一个项目,该项目在控制器中调用从数据库获取数据的操作方法。

我还有一个按钮,我希望用户能够在网格中选中一个复选框,然后单击提交按钮将复选框的值传递给控制器​​中的操作方法。

问题是,当我从下拉列表中选择一个项目时,它会调用提交按钮“DiscontinueProduct”的操作方法, 不是dropdownlist的动作方法(“GetProductByID”),有人可以告诉我我做错了什么吗? 这是我的代码。

提前致谢。

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

视图

<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

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

控制器

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

1 个答案:

答案 0 :(得分:0)

您似乎必须阻止下拉列表中的SUBMIT操作。看看How to prevent buttons from submitting forms