1

我正在用 asp.net MVC 2.0 开发一个示例。在我的视图 (ProductDetails.aspx) 中,我有 3 个链接按钮,它们调用同一控制器 (ProductController) 中存在的不同操作方法。当我单击搜索链接时,会调用相应的操作方法,但是当我使用字符串从表单集合中获取数据时,我会在pid=Request.Form["tbPid"]pid 中获取一个空值。帮助我纠正问题。

注意:我没有 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");
        }
    }
}
4

1 回答 1

1

Html.ActionLink生成锚 ( a) 标记,单击该标记时,只需将浏览器指向不同的地址。您需要的是一个提交按钮,它可以将表单数据发布到服务器。Html.BeginForm提交表单数据的url可以在视图渲染时通过-的参数来控制,也可以通过客户端的javascript作为标签的action属性值来控制。form如果您没有指定Html.BeginForm参数(如在您的代码中),表单将被发布到与生成页面的名称相同的控制器操作。您需要创建一个重载的操作方法(FormCollection collection例如带有参数)并用[HttpPost]属性标记它。

于 2013-04-30T13:51:12.977 回答