Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Arucha Rungchokanun 5 posts 50 karma points
    Jul 30, 2014 @ 06:50
    Arucha Rungchokanun
    0

    Child action is called instead of HttpPost action when submit button is click?

    HI,

    Using Umbraco 7.1.4 I've created myself a SurfaceController to test out the creation of new members and logging in.I don't know why child action is called instead of httppost action when submit button is click.

    Here is all my code:

    test\Controllers\RegistrationLoginSurfaceController.cs

     public class RegistrationLoginSurfaceController : SurfaceController
    {

    [ChildActionOnly]
    public ActionResult Register()
    {
    return PartialView("RegistrationLoginView", new RegistrationLoginViewModel());
    }

    [NotChildAction]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegistrationLoginViewModel U, string Command)
    {
    if (Command == "Register")
    {
    //do something

    if (ModelState.IsValid)
    {
    //save to DB
    return PartialView("AfterRegisterView", U);
      }

    }
    else
    {
    return RedirectToUmbracoPage(id);
    }
    }

    test\Views\Partials\RegistrationLoginView.cshtml

    @model test.ViewModel.RegistrationLoginViewModel

    @using (Html.BeginUmbracoForm("Register"))
    {
    @Html.ValidationSummary(true)

    <fieldset>
    @Html.AntiForgeryToken()
    @if (ViewBag.Message != null)
    {
    <div style="border:solid 1px green">
    @ViewBag.Message
    </div>
    }
    <form role="form">
    <div class="form-horizontal">
    <div class="form-group">
    <div class="col-sm-offset-3 col-sm-9">
    <button type="submit" name="Command" value="Register" class="btn btn-default">Register</button>
    <button type="submit" name="Command" value="Cancel" class="btn btn-default">Cancel</button>
    </div>
    </div>
    </div>
    </form>

    </fieldset>
    }

    test\Views\Registration.cs

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
    Layout = "WeProDefaultSidebarcshtml.cshtml";
    }

    @Html.Action("Register", "RegistrationLoginSurface")

    Any suggestion?

  • Dan Lister 416 posts 1974 karma points c-trib
    Jul 30, 2014 @ 10:37
    Dan Lister
    0

    Hi Arcuha,

    Try changing the method name of your HttpPost method. I have a feeling it has to be different than the HttpGet method name.

    Thanks, Dan.

  • Arucha Rungchokanun 5 posts 50 karma points
    Jul 30, 2014 @ 16:46
    Arucha Rungchokanun
    0

    Hi Dan,

    Thank you for your quick reply.

    There is nothing change.

    I've changed HttpPost action from "Register" -> "RegisterSomeSuffix".

    It still call child action instade of HttpPost. T^T

    Any other clue?

  • Hywel Rees 56 posts 224 karma points
    Jul 31, 2014 @ 10:51
    Hywel Rees
    0

    Hi Arucha,

    Your method names are OK as they are, because the signatures between the two are different. Your problem looks like your form submit button is performing an Http GET. You need it to perform an Http POST.

    I would fix this by removing the

    tags in your view, and replacing them with BeginUmbracoForm, as follows:

    @using (Html.BeginUmbracoForm("Register", "RegistrationLoginSurface", new { role = "form" })) 
    {
            <div class="form-horizontal">
                <div class="form-group">
                    <div class="col-sm-offset-3 col-sm-9">
                        <button type="submit" name="Command" value="Register" class="btn btn-default">Register</button>
                        <button type="submit" name="Command" value="Cancel" class="btn btn-default">Cancel</button>
                    </div>
                </div>
            </div>
    }
    

    This should do the trick!

    Cheers,

    HJR

  • Arucha Rungchokanun 5 posts 50 karma points
    Jul 31, 2014 @ 12:13
    Arucha Rungchokanun
    0

    Hi Hywel,

    Thank you for your trick.

    I've tried that and finally found out the problem is caused by html <form> tag as you mentioned.

    @using (Html.BeginUmbracoForm<ICB2015_U7.Controllers.RegistrationLoginSurfaceController>("Register"))//this is work fine
    @using (Html.BeginUmbracoForm("Register", "RegistrationLoginSurface", new { role = "form" }))
    //this is work fine
    @using
    (Html.BeginUmbracoForm("Register","RegistrationLoginSurface")) //this is work fine
    {
           
    <form role="form">//i've removed this
    //some html helper
    .
    .
    .
    </form>//i've removed this
    }

    Anyway, I still wonder "What is the difference between submit with <form> and without <form>?"

    And this render action code

    @using (Html.BeginUmbracoForm("Register", "RegistrationLoginSurface", new { role = "form" }))

    means submit with <form>, isn't it?

    If the answer is yes, thus, these render action codes

    @using (Html.BeginUmbracoForm("Register", "RegistrationLoginSurface"))

    and

    @using (Html.BeginUmbracoForm<ICB2015_U7.Controllers.RegistrationLoginSurfaceController>("Register"))

    are submit without <form>, aren't they?

     

    Thank you very much again.

    Cheers,

    Arucha

  • Hywel Rees 56 posts 224 karma points
    Jul 31, 2014 @ 12:59
    Hywel Rees
    0

    Hi Arucha,

    The BeginUmbracoForm helper method is just another way of writing a form tag. If you view the source code of your website, you will probably see something like this in place of your @using (Html.BeginUmbracoForm) code:

    <form action="/" enctype="multipart/form-data" method="post">
        <!-- some stuff -->
    </form>
    

    In your example, you wanted to specifically hit the [HttpPost] action in your controller. However, because your form tag did not specify a method, the default of HttpGet was being used. You could either do this using the helper:

    @using (Html.BeginUmbracoForm("Register", "RegistrationLoginSurface")) 
    

    Or

    @using (Html.BeginUmbracoForm("Register", "RegistrationLoginSurface", FormMethod.Post)) 
    

    Or, an equally valid fix would have been to add a method attribute to your form tag:

    <form role="form" method="post>
    

    You can do it either way, but I tend to prefer using the helper methods if I can.

    Cheers,

    Hywel

  • Arucha Rungchokanun 5 posts 50 karma points
    Jul 31, 2014 @ 18:40
    Arucha Rungchokanun
    0

    Hi Hywel,

    Thank you for your reply.

    It make me understand more clearly.

  • Arucha Rungchokanun 5 posts 50 karma points
    Jul 31, 2014 @ 18:48
    Arucha Rungchokanun
    0

    Inaddition, in httppost action method, I've changed return partialview() to return CurrentUmbracoPage()

    follow this link http://our.umbraco.org/documentation/Reference/Mvc/forms

     

Please Sign in or register to post replies

Write your reply to:

Draft