【问题标题】:mvc3 html.Dropdownlist() and html.beginform()mvc3 html.Dropdownlist() 和 html.beginform()
【发布时间】:2011-02-14 14:44:02
【问题描述】:

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

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

问题是当我从下拉列表中选择一个项目时,它会调用提交按钮“DiscontinueProduct”的操作方法,并且 不是下拉列表的操作方法(“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

【问题讨论】:

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


    【解决方案1】:

    看来您必须阻止下拉列表的 SUBMIT 操作。看看这里How to prevent buttons from submitting forms

    【讨论】:

      猜你喜欢
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      相关资源
      最近更新 更多