Copied to clipboard

Flag this post as spam?

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


  • Jay 49 posts 211 karma points
    Dec 31, 2014 @ 10:51
    Jay
    0

    How To Send Along Non-Model Data From Html.BeginForm?

    Umbracians,

    I am building a site in Umbraco 7, using MVC4. I have a working form from which I can successfully post data back to the controller.

    However, I would like to include two radio buttons on the form that are not bound to the model.

    The radio buttons will let the user pick whether their last name is equal to their birth name. If true, then I want to send this along to the controller, so that I can assign the LastName property to the BirthName property.

    In my controller, I have the following code:

    public RedirectResult Submit(User user, Dictionary<string, object> htmlAttributes)
    {
        // some code...
        return Redirect(this.Request.UrlReferrer.ToString());
    }
    

    In my razor view, I have the following code:

    @using (Html.BeginForm("Submit", "MemberAddressForm", FormMethod.Post, new { Name = "Jay" }))
    {
        // strongly typed, working form declaration
    
        // two non-model radio buttons
    <label for="rbLastNameEqualsBirthNameTrue">Yes</label>
    <input type="radio" id="rbLastNameEqualsBirthNameTrue" name="lastNameEqualsBirthName" value="true" checked="checked" />
    <label for="rbLastNameEqualsBirthNameFalse">No</label>
    <input type="radio" id="rbLastNameEqualsBirthNameFalse" name="lastNameEqualsBirthName" value="false" />
    
    }
    

    My first problem is that I am not getting key/value pair {Name="Jay"} in my controller htmlAttributes parameter. It does hold two parameters, but those are for the controller name and the controller action. Why is my own custom key/value pair not being added? Am I not accessing my html attributes in the right way?

    My second problem is that I need to figure out how to send along the non-model radiobuttons' value, rather than my own hard coded name. How can I do this?

    Is what I'm trying to do even possible? If so, is this also the best practice? If not, then how should I do it? Should I modify the model?

  • Jay 49 posts 211 karma points
    Dec 31, 2014 @ 13:26
    Jay
    0

    Okay, so I figured out I can pass a parameter using Html.RenderAction, just so long as the passed parameter's name matches the one I've got setup in my routing.

    However, it's still not working for Html.BeginForm.

    Also... I have a new requirement:

    The member address form is used on multiple pages. Based on which page the form is used on, it needs to redirect to a specific confirmation page.

    So PageA.aspx needs to redirect to PageA-Thanks.aspx and PageB.aspx needs to redirect to PageB-Thanks.aspx.

    What's the best practice for accomplishing this without having to hardcode any such redirects? I would prefer it if the user would be able to set a RedirectPage property on PageA, so that my code could make use of it to perform the proper redirect.

    How do I do this? Should I pass along @Umbraco.Field("RedirectPage") to my Html.RenderAction, which passes it on via the ViewBag to the member address form, which then posts it back to the controller, which then performs the proper redirect?

  • Jay 49 posts 211 karma points
    Jan 02, 2015 @ 10:13
    Jay
    0

    I'm just gonna bump this to see if I can get some replies now that we've had new years...

  • Jay 49 posts 211 karma points
    Jan 02, 2015 @ 12:31
    Jay
    100

    Never mind, I already figured it out.

    For those of you struggling with the same problem, here's how to pass parameters from view to controller via submit.

    Controller:

    public ActionResult Submit(User newUser, string someParam)
    {
    }
    

    View:

    @using (Html.BeginForm("Submit", "MemberAddressForm", new { someParam = "some value" }, FormMethod.Post))
    {
    }
    

    The above BeginForm call puts parameters in the generated form's action element. These parameters are matched by name to the called controller action.

  • Jay 49 posts 211 karma points
    Jan 02, 2015 @ 14:10
    Jay
    0

    I've also figured out how to make a certain page redirect to a certain other page.

    In my controller index I pass along the current page's ID to the partial view:

    public ActionResult Index()
    {
        InitUserSessionByUniqueCode();
    
        ViewBag.PageID = this.CurrentPage.Id;
    
        return View("/Views/MacroPartials/MemberAddressForm.cshtml", state.CampagneSessionData);
    }
    

    In my partial view, I pass the page ID back to the controller like so:

    @using (Html.BeginForm("Submit", "MemberAddressForm", new { pageID = ViewBag.PageID }, FormMethod.Post))
    {
    }
    

    The controller's submit action can now retrieve the desired redirection page ids from the current page's properties, like so:

    public ActionResult Confirmation(int pageID)
    {
        IContent currentPage = Services.ContentService.GetById(pageID);
    
        int redirectionPageID = Convert.ToInt32(currentPage.GetValue("RedirectConfirmationPageID"));
    
        return RedirectToUmbracoPage(redirectionPageID);
    }
    

    You're welcome! :D

  • luke 1 post 71 karma points
    May 08, 2016 @ 10:46
    luke
    0

    Hello,

    Did you figure out how to use Html.BeginForm to send along the non-model radiobuttons' value, rather than my own hard coded name?

    Thank you

Please Sign in or register to post replies

Write your reply to:

Draft