Html 使用MVC2.0从操作方法检索表单集合数据
我正在asp.NETMVC2.0中开发一个示例。在我的视图(ProductDetails.aspx)中,我有3个链接按钮,它们调用同一控制器(ProductController)中存在的不同操作方法。当我单击搜索链接时,会调用相应的操作方法,但当我使用字符串Html 使用MVC2.0从操作方法检索表单集合数据,html,asp.net-mvc,formcollection,Html,Asp.net Mvc,Formcollection,我正在asp.NETMVC2.0中开发一个示例。在我的视图(ProductDetails.aspx)中,我有3个链接按钮,它们调用同一控制器(ProductController)中存在的不同操作方法。当我单击搜索链接时,会调用相应的操作方法,但当我使用字符串pid=Request.Form[“tbPid”]从表单集合中获取数据时,我会在pid中获取一个空值。帮我纠正这个问题 注意:我没有AddProduct链接的代码,但我认为不会有问题 查看代码: <%@ Page Language="C
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");
}
}
}
产品详细信息
pid:
pname:
最小数量:
最大数量:
向上:
Pname:
最小数量:
最大数量:
向上:
制造商:
**控制器代码:**
命名空间MVCProductDetailsDemo.Controllers
{
公共类ProductController:控制器
{
//常规方法和操作方法。
//ActionMethods是通常调用视图的方法。
公共行动结果显示产品()
{
列表lstmanifactors=GetManifacturers();
ViewData[“制造商”]=新的选择列表(第一制造商);
返回视图(“产品详细信息”);
}
公共列表GetManifacturers()
{
制造商制造商=新制造商();
返回manifacturer.GetManifacturers();
}
公共行动结果搜索产品()
{
字符串pid=Request.Form[“tbPid”];
产品=新产品();
ProductModel pModel=products.GetProductDetails(Convert.ToInt32(pid));
列表lstmanifactors=GetManifacturers();
ViewData[“制造商”]=新的选择列表(第一制造商);
返回视图(“ProductDetails”,pModel);
}
公共操作结果更新产品()
{
字符串pid=Request.Form[“tbPid”];
字符串pname=Request.Form[“tbPname”];
字符串minqty=Request.Form[“tbMinQty”];
字符串maxqty=Request.Form[“tbMaxQty”];
string up=Request.Form[“tbUP”];
产品=新产品();
ProductModel pModel=新ProductModel();
pModel.Mid=1;
pModel.PName=PName;
pModel.MinOrdQty=转换为32(minqty);
pModel.MaxOrdQty=转换为32(maxqty);
pModel.Pid=Convert.ToInt32(Pid);
pModel.UnitPrice=Convert.ToInt32(向上);
bool isRecordUpdated=products.UpdateProducts(pModel);
列表lstmanifactors=GetManifacturers();
ViewData[“制造商”]=新的选择列表(第一制造商);
返回视图(“ProductDetails”,pModel);
}
公共行动结果删除产品()
{
字符串pid=Request.Form[“tbPid”];
产品=新产品();
bool isRecordDeleted=products.DeleteProduct(Convert.ToInt32(pid));
列表lstmanifactors=GetManifacturers();
ViewData[“制造商”]=新的选择列表(第一制造商);
返回视图(“产品详细信息”);
}
}
}
Html.ActionLink
生成锚(一个
)标记,单击该标记时,只需将浏览器指向不同的地址。您需要的是一个submit按钮,用于将表单数据发布到服务器。提交表单数据的url可以在视图呈现期间通过Html.BeginForm
-的参数控制,或者通过客户端上的javascript作为表单
标记的操作
属性的值来控制。如果未指定Html.BeginForm
paremeters(如代码中所示),则表单将以与生成页面的表单相同的名称发布到控制器操作。您需要创建一个重载操作方法(例如,使用参数FormCollection
)并使用[HttpPost]
属性标记它 Html.ActionLink
生成锚(a
)标记,单击该标记时,只需将浏览器指向不同的地址。您需要的是一个submit按钮,用于将表单数据发布到服务器。提交表单数据的url可以在视图呈现期间通过Html.BeginForm
-的参数控制,或者通过客户端上的javascript作为表单
标记的操作
属性的值来控制。如果未指定Html.BeginForm
paremeters(如代码中所示),则表单将以与生成页面的表单相同的名称发布到控制器操作。您需要创建一个重载操作方法(例如,使用参数FormCollection
)并使用[HttpPost]
属性标记它 谢谢你的快速回复。你能给我一个样品吗?谢谢你的帮助。它正在工作,我在视图中使用了控件,我正在从控制器获取按钮数据。感谢您的快速回复。你能给我一个样品吗?谢谢你的帮助。它正在工作,我在视图中使用了控件,我正在从控制器获取按钮数据。