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

4
  • 1
    You have a type on the AcceptVerbs attribute for the post. ".Posr" -> ".Post" Commented Jul 3, 2012 at 16:14
  • This is a duplicate of stackoverflow.com/questions/283209/… Commented Jul 3, 2012 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 Commented Jul 4, 2012 at 7:29
  • if you comment out the GET Create method, how are you gonna post then form back to POST Create method? Commented Jul 11, 2012 at 13:01

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.