1

When i add 2 Create Method in my controller like below

        [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Create()
    {
        Product.Models.Product p = new Models.Product();
       //update DB
        try
        {
            return RedirectToAction("GetAll");
        }
        catch (Exception)
        {

            return View(p);
        }

    } 

    //
    // POST: /Product/Default1/Create

     [AcceptVerbs(HttpVerbs.Posr)]
    public ActionResult Create(FormCollection collection)
    {
        try
        {
            if (myProduct.Products == null)
            {
                myProduct.Products = new List<Models.Product>();
            }
            Product.Models.Product p = new Product.Models.Product();
            p.Name =  collection["Name"];
            p.ProductType = collection["ProductType"];
            p.Id = myProduct.Products.Count + 1;

            myProduct.Products.Add(p);


            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

If i Comment GET action verbs and run application, application throws error resource not found. It will not fire Create Action. my html has @using (Html.BeginForm())

I Changed Form to even then i get same error. If i uncomment GET action verb, then always GET method fires up. I want call POST Create action . Can any1 directme how to solve.

I have Product has Areas in my MVC project. within in it has ProductsController.cs Please help me how to call POST action Create method.

-Mahender

Styxxy
  • 7,462
  • 3
  • 40
  • 45
user145610
  • 2,949
  • 4
  • 43
  • 75
  • 1
    You have a type on the AcceptVerbs attribute for the post. ".Posr" -> ".Post" – Brian S Jul 03 '12 at 16:14
  • This is a duplicate of http://stackoverflow.com/questions/283209/asp-net-mvc-acceptverbs-and-registering-routes – HatSoft Jul 03 '12 at 16:14
  • I was able to understand meaning of having 2 methods with different verbs in ASP.NET MVC. But the question is how to force always use the POST in MVC. Is there any HTTP Constraint , I need to set in routing strategy – user145610 Jul 04 '12 at 07:29
  • if you comment out the GET Create method, how are you gonna post then form back to POST Create method? – patel.milanb Jul 11 '12 at 13:01

2 Answers2

1

Try following:

@using (Html.BeginForm("Create", "Default1", new { area = "Product" }, FormMethod.Post)) {
// Your Form
}

And please use

[HttpGet]
[HttpPost]

instead of AcceptVerbs, its much cleaner.

l1e3e3t7
  • 124
  • 11
0
[HttpGet] // Or even if you dont put this, it will be treated as GET
public ActionResult Create()
{
     // your code goes here
}

[HttpPost]
public  ActionResult Create(Model yourModel)
{
    // your code goes here 
}

in your view 
@using(Html.BeginForm("HandleForm", "Home")
patel.milanb
  • 5,822
  • 15
  • 56
  • 92