2011-02-14 93 views
0

嗨,大家好我在我的視圖中有一個下拉列表和一個提交按鈕。 我希望用戶能夠在下拉列表中選擇一個項目,以調用從數據庫獲取數據的控制器中的操作方法。mvc3 html.Dropdownlist()和html.beginform()

我也有一個按鈕,我希望用戶能夠選擇網格中的複選框,然後單擊提交按鈕將複選框的值傳遞給控制器​​中的操作方法。

問題是,當我從下拉列表中選擇一個項目它要求提交按鈕「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 

回答