2

Problem

I have Telerik TabControl and each tab content is a partial view. Everything works smoothly when request is GET:

//
// GET: /ProductVersion/Translations        
public ActionResult Translations(Guid id)
{
    VersionEditTabViewModel model = CreateTranslationsViewModel(id);
    return PartialView("Translations", model);
}

Now the problem is that on some tabs I have a Form that has controls that trigger submit event.

[HttpPost]
public ActionResult Translations(Guid id)
{
    FormCollection formCollection = new FormCollection(Request.Form);
    string message = string.Empty;
    int languageId = int.Parse(formCollection["TranslationsLanguageList"]);
    string action = formCollection["TranslationAction"];
    if(action == Constants.translation_save)
    {
        _translationModel.SaveTranslations(formCollection);
        message = "Translation information saved";
    }
    else if (action == Constants.translation_language_changed)
    {
/*
    PROBLEM: causes whole page to render, not partial
*/
        return PartialView("Translations", model);
    }
    return RedirectToAction( ... updates the complete page not only partial ...);
}

My question is: how to render partial from the POST method? Because now with that source code tab content will be loaded to the WHOLE page, not inside tab.

Solution

I had to put DIV outside of the Ajax.Form and also I had incorrect submit on my DropDownList. What I did was that I created hidden submit button with Id and then I used jQuery to execute it's click event.

2 Answers 2

3

For additional reference, please refer to this question on SO:

MVC - Using Ajax to render partial view

This shows a complete implementation of the Ajax.BeginForm with surrounding DIV and inner form controls. You should be able to place this entire setup (DIV + Form + HTML Form Elements) in the Telerik Tab, like this:

<% Html.Telerik().TabStrip()
        .Name("TabStrip")
        .Items(tabstrip =>
        {
            tabstrip.Add()
                    .Text("Your Tab Text")
                    .Content(() =>
                    {%>
                        <div id="containerDiv" align="left">
                           <% using (Ajax.BeginForm("Example", "Controller/Action", new AjaxOptions { UpdateTargetId = "containerDiv" })){ %>
                           <%-- Render Partial here -->
                           <% } %>
                        </div>
                    <%});

Hope that helps.

Sign up to request clarification or add additional context in comments.

2 Comments

I checked the link you posted. I made a little test and added a submit button instead of having only a DropDownList with submit added using jQuery. For some reason submit button behaves differently than DropDownList when submit is done. I have to investigate little bit more. I'll let you know when I solve it (finally)
I had other mistakes too like you pointed in the example. Div must be outside of the Ajax.BeginForm. Big thanks to Todd!
1

I did my trough ajax form:

using (Ajax.BeginForm("*ActionName*", new { *parameter = ID* }, new AjaxOptions { UpdateTargetId = (*div i will update*), OnSuccess = "*JavaScript that executes on success*", OnComplete = "s*ame as on success*", InsertionMode = InsertionMode.Replace }))

and then i have

return PartialView("*PartialViewName*", model);

in post Action

And it works just fine, on post, action returns partial view and then ajax form replaces the content of the div specified in the UpdateTargetId with InsertionMode.Replace

3 Comments

Thank you for your answer. I didn't get working yet. This is what I added: <% using (Ajax.BeginForm("Translations", new { Model.Id }, new AjaxOptions { UpdateTargetId = "translationContent", InsertionMode = InsertionMode.Replace })) { %> <div style="width: 100%; height: 100%;" id="translationContent"> ... blaa blaa content blaa .. </div> <% } %> On the controller side I have: return PartialView("Translations", model);
It's still changing the whole content of the page
my update target id is a div wrapping the whole form and i got it like this <div id="content"> <% Html.RenderPartial("PartialView",model); </div> then when i do post, return is a partial view, so you need to replace the old rendered partial view with the new one :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.