2013-04-30 28 views
1

我正在開發一個asp.net MVC 2.0中的示例。在我的視圖(ProductDetails.aspx)中,我有3個鏈接按鈕,它們調用同一控制器中存在的不同操作方法(ProductController)。當我點擊搜索鏈接時會調用相應的操作方法,但是當我使用字符串pid=Request.Form["tbPid"]從表單集合中獲取數據時,我將pid中的值設爲空值。幫助我糾正這個問題。使用mvc 2.0從操作方法中檢索表單集合數據

注意:我沒有AddProduct鏈接代碼,但我認爲沒有問題。

代碼查看:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MVCProductDetailsDemo.Models.ProductModel>" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>ProductDetails</title> 
</head> 
<body> 
    <div> 
     <% using(Html.BeginForm()){ %> 
     pid:<%=Html.TextBox("tbPid") %> 
     <%=Html.ActionLink("Search Product", "SearchProducts", "Product")%><br /> 
     <%if (Model != null) 
      { %> 
     pname:<%=Html.TextBox("tbPname", Model.PName)%><br /> 
     Minqty:<%=Html.TextBox("tbMinQty",Model.MinOrdQty) %><br /> 
     Maxqty:<%=Html.TextBox("tbMaxQty",Model.MaxOrdQty) %><br /> 
     Up:<%=Html.TextBox("tbUP",Model.UnitPrice) %><br /> 
     <%} %> 
     <%else { %> 
       Pname:<%=Html.TextBox("tbPname") %> 
       MinQty:<%= Html.TextBox("tbMinQty")%> 
       MaxQty:<%=Html.TextBox("tbMaxQty")%> 
       UP:<%=Html.TextBox("tbUP") %> 
      <%} %> 
     Manifacturer:<%=Html.DropDownList("manifacturers") %><br /> 




     <%=Html.ActionLink("Add Product","AddProduct","Product") %> 
     <%=Html.ActionLink("Upd Product","UpdateProduct","Product") %> 
     <%=Html.ActionLink("Del Product","DeleteProduct","Product") %> 
     <%} %> 
    </div> 
</body> 
</html> 

**Code for Controller:** 
namespace MVCProductDetailsDemo.Controllers 
{ 
    public class ProductController : Controller 
    { 
     //Normal Methods and ActionMethods. 
     //ActionMethods are the methods which will usually call the views. 
     public ActionResult DisplayProduct() 
     { 
      List<string> lstManifacturers=GetManifacturers(); 
      ViewData["manifacturers"] = new SelectList(lstManifacturers); 
      return View("ProductDetails"); 
     } 
     public List<string> GetManifacturers() 
     { 
      Manifacturers manifacturer = new Manifacturers(); 
      return manifacturer.GetManifacturers(); 
     } 
     public ActionResult SearchProducts() 
     { 
      string pid=Request.Form["tbPid"]; 
      Products products = new Products(); 
      ProductModel pModel=products.GetProductDetails(Convert.ToInt32(pid)); 
      List<string> lstManifacturers = GetManifacturers(); 
      ViewData["manifacturers"] = new SelectList(lstManifacturers); 
      return View("ProductDetails", pModel); 
     } 
     public ActionResult UpdateProduct() 
     { 
      string pid = Request.Form["tbPid"]; 
      string pname = Request.Form["tbPname"]; 
      string minqty = Request.Form["tbMinQty"]; 
      string maxqty = Request.Form["tbMaxQty"]; 
      string up = Request.Form["tbUP"]; 
      Products products = new Products(); 
      ProductModel pModel = new ProductModel(); 
      pModel.Mid = 1; 
      pModel.PName = pname; 
      pModel.MinOrdQty = Convert.ToInt32(minqty); 
      pModel.MaxOrdQty = Convert.ToInt32(maxqty); 
      pModel.Pid = Convert.ToInt32(pid); 
      pModel.UnitPrice = Convert.ToInt32(up); 
      bool isRecordUpdated = products.UpdateProducts(pModel); 
      List<string> lstManifacturers = GetManifacturers(); 
      ViewData["manifacturers"] = new SelectList(lstManifacturers); 
      return View("ProductDetails", pModel); 
     } 
     public ActionResult DeleteProduct() 
     { 
      string pid = Request.Form["tbPid"]; 
      Products products = new Products(); 
      bool isRecordDeleted = products.DeleteProduct(Convert.ToInt32(pid)); 
      List<string> lstManifacturers = GetManifacturers(); 
      ViewData["manifacturers"] = new SelectList(lstManifacturers); 
      return View("ProductDetails"); 
     } 
    } 
} 

回答

1

Html.ActionLink產生錨(a)標籤,點擊後,只需要點瀏覽器到不同的地址。你需要的是一個提交按鈕,可以將表單數據發佈到服務器上。表單數據提交的URL可以通過參數Html.BeginForm - 在視圖呈現過程中或通過客戶端上的javascript作爲form標記的action屬性的值來控制。如果您沒有指定Html.BeginForm參數(與您的代碼一樣),則表單將發佈到控制器操作,其名稱與生成該頁面的操作相同。您需要創建一個重載的操作方法(例如,參數FormCollection collection),並用[HttpPost]屬性標記它。

+0

感謝您的快速回復。你能給我一個樣本嗎? – user2336064 2013-04-30 15:32:34

+0

感謝您的幫助。它正在工作,我在視圖中使用了