我正在开发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");
}
}
}
答案 0 :(得分:1)
Html.ActionLink
生成锚点(a
)标记,单击该标记时,只需将浏览器指向其他地址即可。您需要的是一个提交按钮,可以将表单数据发布到服务器。提交表单数据的网址可以通过Html.BeginForm
的参数在视图呈现期间控制,也可以通过客户端上的javascript控制为action
标记的form
属性的值。如果未指定Html.BeginForm
参数(如代码中所示),则表单将发布到控制器操作,其名称与生成页面的名称相同。您需要创建一个重载的操作方法(例如,使用参数FormCollection collection
)并使用[HttpPost]
属性标记它。