Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
[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
HTML
<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> 


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
Posted
Updated 21-Feb-18 19:39pm
Comments
Jitendra Sabat 20-Aug-13 1:17am    
I guess, your form submission do not take place correctly.The data you sent to server are not available at particular page during submissioin,that's why it is by default taking "None" as it's value as you have provided "None" as an optional value.
Member 11083196 16-Sep-14 1:04am    
m

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'. ;)
 
Share this answer
 
Add below of these in Update Action Method::

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]




<input type="button" command="Clear"/>


C#
if(command=="Add10")
{

//Your Logoc
}




C#
if(command=="Add10")
{
//Your Logic
}




give the above command in view Button..!!!
 
Share this answer
 
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.
 
Share this answer
 
Check Your Controller is stating that it's none therefore it's returning None vs. your view where you're giving values Add10, etc.

C#
[HttpPost]
        public ActionResult Index(string method ="None" )
        {
 
Share this answer
 
Comments
Deepu S Nair 22-Feb-18 1:45am    
You are answering a question which is nearly 6 years old and already solved .It may attract downvoting your answer.Please try to answer new questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900