MVC将帖子上的参数传递给控制器

MVC将帖子上的参数传递给控制器

本文介绍了MVC将帖子上的参数传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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

推荐答案


[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..!!!



这篇关于MVC将帖子上的参数传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:54