0

I'm trying to call an action in a separate controller with an actionlink.

The problem is that both the [HttpGet] and the [HttpPost] action gets called, and since the post-method returns the view from which the action is called, nothing gets displayed.

Get method:

[HttpGet]
        public ActionResult View(int id, int index)
        {
            var form = formService.GetForm(id);
            var pageModel = new PageViewModel();

            var page = form.Pages.ElementAt(index);

            ModelCopier.CopyModel(page, pageModel);
            ModelCopier.CopyModel(form, pageModel);

            return View(pageModel);
        }

Post Method

[HttpPost]
    public ActionResult View(PageViewModel pageViewModel)
    {
        if (!ModelState.IsValid)
        {
            return RedirectToAction("Details", "Forms", new { id = pageViewModel.FormId });
        }
            var pageToEdit = pageService.GetPage(pageViewModel.PageId);

            ModelCopier.CopyModel(pageViewModel, pageToEdit);

            pageService.SavePage();
            return RedirectToAction("Details", "Forms", new {id = pageViewModel.FormId});
    } 

How it's called from the View (from a view returned by another controller):

@Html.ActionLink("View", "View", "Pages", new { id = Model.FormId, index = item.Index-1 }, null)

What am I doing wrong here? I essentially want it to work as an update/edit function. And the view returned contains a simple form for the viewmodel.

tohereknowswhen
  • 199
  • 2
  • 3
  • 9
  • Could you clarify what behavior you expect? What do you mean that both actions are being called? The browser should only be issuing one request. – marcind Apr 05 '11 at 14:02

2 Answers2

0

I don't think you'll be able to use an action link - see this question

The options are to use jQuery to post data or swap to a simple form and submit

Community
  • 1
  • 1
Richard
  • 1,804
  • 16
  • 22
0

An action link issues a GET request. You will have to implement some JavaScript function that captures the url and parameters, and dynamically creates and submits a new form with a POST method, or does an Ajax POST. You could write your own HTML Helper, to wrap this functionality, but the default functionality of clicking an <a> tag (which is what is generated by a Html.ActionLink) will issue a GET request.

smartcaveman
  • 41,281
  • 29
  • 127
  • 212
  • After reading through the thread I've got another question. What I want is a Get Request, which i get, but I also trigger the post action. Am I wrong? – tohereknowswhen Apr 06 '11 at 06:38
  • If you want the controller action to be accessible a GET request, then you need to remove the `HttpPost` attribute. A GET and POST are mutually exclusive, and the intention to somehow do both at once seems to reflect a fundamental misunderstanding of HTTP. The specification document for HTTP is available at http://www.w3.org/Protocols/rfc2616/rfc2616.html. – smartcaveman Apr 06 '11 at 14:25
  • Maybe I explained my problem badly. I wanted a GET but a post action was triggered in the controller. Anyways, I'm using jQuery/Ajax now instead, and It's working. – tohereknowswhen Apr 08 '11 at 08:25