vendredi 16 septembre 2016

Model is null on postback in ajax loaded partial view

I'm using the following pattern http://ift.tt/L1fUzz 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

Aucun commentaire:

Enregistrer un commentaire