MVC将帖子上的参数传递给控制器 [英] MVC passing parameter on post to controller
问题描述
我是MVC的新手
i需要一些帮助来解决在表单提交时将参数传递给控制器的问题
我得到的是以下控制器和视图
i am very new to MVC
i need some help to over come the issue of passing parameter to a controller on form submit
what i have got is the following controller and the view
[HttpGet]
public ActionResult Index()
{
return View(_bag.GetBag);
}
[HttpPost]
public ActionResult Index(string method ="None" )
{
switch (method)
{
case "Add10":
_bag.GetBag = Get100Products().Take(10).ToList<Product>();
break;
case "Clear":
_bag = null;
_bag = new Models.Bag();
break;
case "Add":
if ((Request.Form["Id"] != null) && (Request.Form["Id"] != ""))
{
if (_bag.GetBag.Count < 100)
{
var p = GetProduct(Request.Form["Id"]);
int qnt = Convert.ToInt16(Request.Form["qnt"]);
if (p.ItemNumber != null)
{
p.Quantity = qnt;
p.Index++;
_bag.Item = p;
}
}
}
break;
}
return View(_bag.GetBag);
}
和视图的视图部分
and the view part of the view
<div style="vertical-align:middle">
@using (Html.BeginForm("", "Home", new { method = "Add10" }, FormMethod.Post))
{
<!-- form goes here -->
<input type="submit" value="Add 10 Items to bag" />
}
@using (Html.BeginForm("GetDiscount", "Home", FormMethod.Post))
{
<div>
<!-- form goes here -->
<input type="submit" value="Get Discount" />
With MAX time in seconds <input type="text" name="time" maxlength="2" value="2" />
</div>
}
@using (Html.BeginForm("", "Home", new { method = "Clear" }, FormMethod.Post))
{
<input type="submit" value="Empty the bag" />
}
</div></form>
所以我是在使用时点击按钮将10个项目添加到包中以将方法值Add10传递给索引控制器并在单击时清空包以通过清除索引控制器中的方法值
但它总是显示为无
我做错了什么感谢你的帮助
谢谢
so i am expecting when the use clicked button Add 10 Items to bag to pass the method value "Add10" to the index controller and when clicked Empty the bag to pass "Clear" the method value in index controller
but it always shows as "None"
what i have done wrong appreciate all your help
thanks
推荐答案
嘿,
什么时候你想将任何值传递给action方法然后指定RouteValues。
这是一个例子。 RouteValues用于将值从视图传递到控制器。参数名称应与routevalue类似。
Html.BeginForm(Index,Home,new {@ method =Test},FormMethod。发布)
另外请不要错过Action方法名称'Index'。 ;)
Hey,
When you want to passed any value to action method then specify the RouteValues.
Here is example. The RouteValues is used for passing value from view to controller. The parameter name should be similar as per routevalue.
Html.BeginForm("Index", "Home",new{@method="Test"},FormMethod.Post)
Also don't miss that Action method name 'Index'. ;)
在Update Action Method中添加以下内容::
Add below of these in Update Action Method::
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
<input type="button" command="Clear"/>
if(command=="Add10")
{
//Your Logoc
}
if(command=="Add10")
{
//Your Logic
}
在视图中给出上面的命令按钮.. !!!
give the above command in view Button..!!!
嘿,亲爱的,因为你h发生了aven't使用method参数配置路由。
试试这个。
routes.MapRoute( 默认,
{controller} / {action} / {id},
new {controller =Home,action =Index,method = });
这将在你的Global.asax文件中。
Hey dear Its happening because you haven't configure route with "method" parameter.
try this.
routes.MapRoute( "Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", method= "" } );
this will be in your Global.asax file.
这篇关于MVC将帖子上的参数传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!