Copied to clipboard

Flag this post as spam?

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


  • progproger 52 posts 130 karma points
    Sep 16, 2016 @ 14:17
    progproger
    0

    Model is null on postback in ajax loaded partial view

    Hi,

    I'm using the following pattern https://github.com/filamentgroup/Ajax-Include-Pattern to load partial views through ajax.

    View:

        @using(Html.BeginUmbracoForm("PostContactInformation", "JoiningSurface", null, new Dictionary<string, object> { { "class", "joinform" } })) {
            @Html.AntiForgeryToken()
            <div data-append="@Url.Action("RenderJoiningContactInformation", "JoiningSurface", new { ContentId = CurrentPage.Id })"></div>
        }
    

    With Action:

     public ActionResult RenderContactInformation(int ContentId)
           {
                var viewModel = ContactViewModel();
                viewModel.Content = Umbraco.TypedContent(ContentId);
    
                return PartialView("RenderContactInformation", viewModel);
            }
    

    Loads partial view perfectly.

    // No need to add partial view i think

    Post action works correctly as well:

    public ActionResult PostContactInformation(ContactViewModel model)  
    {
    //code here
        return RedirectToUmbracoPage(pageid);
    }
    

    The problem is, that i need to add model error to CurrentUmbracoPage if it exists in post...

    For example:

    public ActionResult PostContactInformation(ContactViewModel model)  
    {
        ModelState.AddModelError(string.Empty, "Error occurred");
        return CurrentUmbracoPage();
    }
    

    In this case i get null values for current model. And this happens only when i use ajax.

    If i load action synchronously like that:

       @using(Html.BeginUmbracoForm("PostJoiningContactInformation", "JoiningSurface", null, new Dictionary<string, object> { { "class", "joinform" } })) {
                    @Html.AntiForgeryToken()
                    @Html.Action("RenderContactInformation", "JoiningSurface", new { ContentId = CurrentPage.Id })
                }
    

    everything works like it should.

    But i need to use ajax. Is there a correct way to pass values on postback in this case? I know that i can use TempData, but i'm not sure that this is the best approach. Thanks for your patience

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 16, 2016 @ 15:39
    Alex Skrypnyk
    0

    Hi ProgProger,

    Yes, this is a problem of AJAX requests, can you implement js validation?

    Thanks,

    Alex

  • progproger 52 posts 130 karma points
    Sep 16, 2016 @ 16:37
    progproger
    0

    Hi Alex,

    Thanks for your response... Js validation is working already. I re-initialize js validation on ajax request. But i need server side validation as on post there might be an error

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies