3

I have parent view with a form (Ajax.BeginForm) wrapped around a holding div that I want to render partial views in.

On page load I render an initial partial view which consumes my Model.

I need to submit this form client side via ajax and pass the Model and FormCollection to my controller, where I update my model and then return a different partial view

My question - is this even possible? I basically need to mimic the the submit function of the form button submit click - but in javascript - I have failed so many time at what would appear to be a simple thing.

My View

    @Using (Ajax.BeginForm("Submit", "Invest", New AjaxOptions With {.HttpMethod = "Post", .UpdateTargetId = "partialView"}, New With {.id = "formSubmit", .name = "formSubmit"}))

    <ul class="tabs list-unstyled">
       <li class="active" data-tab-target="accountType" data-js="tabSelect">
       <span>Account Type</span>
       <span id="lblAccountType">Choose an account</span>
       </li>
    </ul>

    <div id="partialView">@Html.Partial("_AccountType", Model)</div>

    End Using

My js

     $('*[data-js="tabSelect"]').on().off().on('click', function () {

     var dto = {
                    data: $('#formSubmit').serialize(), * I tried this but didnt work
                }
                GlobalFunctions.callAjaxMethod("/QuickInvestment/SaveAccountType", dto, callback)
                function callback(data) {
                    $('#partialView').html(data);

                }
    });

my Controller

     Function SaveAccountType(data As Model) As ActionResult

       //Update model and pass to next partial

        Return PartialView("_ModelAcc", Model)

    End Function

Any help to solve this would be greatly appreciated.

Thank you

Tim Leigh
  • 51
  • 2
  • I haven't found anything in VB.NET, but I guess that this one in C# will be descriptive enough: http://stackoverflow.com/questions/11644658/how-to-call-a-controller-method-from-javascript-in-mvc3 – varocarbas Dec 03 '15 at 15:28
  • You have to bear in mind the server/client side separation. In ASP.NET, it becomes a bit less clear, but there is still a big deal of a difference: one thing is what is executed on the server (for example: controller methods) and a different story is what is executed on the browser (for example: JavaScript functions which are triggered without form-submission). Both parts cannot communicate between each other. But when a submission occurs the story changes and this is what the link above is about. I clarify all this just in case (firstly your question didn't seem too clear to me). – varocarbas Dec 03 '15 at 15:32
  • I am doing something similar. See the answer to this question. Basically, remove your UpdateTargetId and add the OnSuccess parameter. Point it to a javascript function where your test if your controller action was successful or failed. In that javascript function replace the div with the partial view $('#myDiv').html(result); I can provide more details on what my controller looks like if needed http://stackoverflow.com/questions/12562857/ajax-beginform-with-oncomplete-always-updates-page – Steve Greene Dec 03 '15 at 15:41
  • You already have `Ajax.BeginForm()` which submits your form and updates the view (except that it submits to `Submit()` method of `InvestController`). What is the point of your javascript? –  Dec 03 '15 at 22:56

0 Answers0