Copied to clipboard

Flag this post as spam?

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


  • Tom 120 posts 448 karma points
    Aug 26, 2014 @ 10:08
    Tom
    0

    Partial View async rendering after POST

    I've got a partial view where you can subscribe to a newsletter. When hitting post I would like it to just update the partial with a message. Not full redirect/postback.

    Here is my POST action

           [HttpPost]
            public async Task<ActionResult> HandleSubscription(Models.NewsletterViewModel model)
            {
                if (!ModelState.IsValid)
                {
    
                    return CurrentUmbracoPage();
    
                }
                .......bla bla bla.............
                //Call webapi to get data
                var returnValue = new MyApiController().SignUpNewsletter(model.Email);
                model.ReturnValue = returnValue;
                PartialView("Newsletter", model);
            }
    

    This returns a blank page with the partial. How do I only update the partial?

  • Tom 120 posts 448 karma points
    Aug 26, 2014 @ 10:14
    Tom
    0

    The view looks something like this:

    @model Models.NewsletterViewModel
    
    @using (Html.BeginUmbracoForm<Controllers.NewsletterController>("HandleSubscription", FormMethod.Post))
    {    
        if(Model.ReturnValue != null){
            if (Model.ReturnValue.IsSuccess == true)
            {
                if (Model.ReturnValue.ExtraReturnObj == "Added")
                { 
    
                    <div>
                        <span>Tak for din tilmelding!</span>
                    </div>
                }
                if (Model.ReturnValue.ExtraReturnObj == "Removed")
                {
                    <div>
                        <span>Nyhedsbrev afmeldt.</span>
    
                    </div>
                }
            }
            else{
                <span>Ups! Noget gik galt! Du kunne ikke tilmeldes nyhedsbrevet!</span>
            }
        }
        if(Model.ReturnValue == null)
        {
        <div>
            <div>
                <h2>@text</h2>
            </div>
    
            <div>
                @Html.TextBoxFor(m => m.Email, new { placeholder = "Email" })
                <div>
                @Html.ValidationMessageFor(m => m.Email)
                </div>
            </div>
            <div>
                <input type="submit" class="theme_button" value="Tilmeld">
            </div>
    </div>
    
  • Tom 120 posts 448 karma points
    Aug 26, 2014 @ 10:17
    Tom
    0

    And of course there should be a "return" before PartialView("Newsletter", model); in the first post.

Please Sign in or register to post replies

Write your reply to:

Draft