Copied to clipboard

Flag this post as spam?

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


  • Matthew Southern 2 posts 72 karma points
    Aug 07, 2016 @ 20:12
    Matthew Southern
    0

    Ajax.BeginForm posts to surface controller and updates target id with full page instead of partial view

    Have been trying to get a member profile management area working with ajax as each section of the page is hidden within a show hide div.

    I have used ajax before in MVC applications but have never used it with umbraco surface controllers before. I'm unsure why returning a partial view in the controller is outputting the whole page and not just the partial view that I am giving to it.

    Controller:

            [HttpPost]
            [ActionName("MvcMemberEditProfileDetails")]
            public ActionResult MvcMemberEditProfileDetails(MvcMemberEditProfileDetailsModel model)
            {
    
                var memberService = Services.MemberService;
                var currentUser = Membership.GetUser();
                var member = memberService.GetByEmail(currentUser.Email);
    
                bool result = false;
    
                if (ModelState.IsValid)
                {
    
                    ...
    
                }
    
                if (result)
                {
                    ...
                }
    
    
                return PartialView("MvcMemberEditProfileDetails", model);
    
            }
    

    View:

    @model Umbraco714.Models.MvcMemberEditProfileDetailsModel
    
    
    @using (Ajax.BeginForm("MvcMemberEditProfileDetails", "MvcMember", new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "MvcMemberEditProfileDetails", InsertionMode = InsertionMode.Replace }))
    {
    
        if (Model.Result != null)
        {
    
            if (Model.Result == true)
            {
    
                <div id="result" class="alert alert-success">
                    @Html.Raw(Model.ResultMessage)
                </div>
    
            }
            else
            {
    
                <div id="result" class="alert alert-danger">
                    @Html.Raw(Model.ResultMessage)
                </div>
    
            }
    
        }
    
        <div class="form-default">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        @Html.LabelFor(m => m.FirstName)
                        @Html.TextBoxFor(m => m.FirstName)
                        @Html.ValidationMessageFor(m => m.FirstName)
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        @Html.LabelFor(m => m.LastName)
                        @Html.TextBoxFor(m => m.LastName)
                        @Html.ValidationMessageFor(m => m.LastName)
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        @Html.LabelFor(m => m.CompanyName)
                        @Html.TextBoxFor(m => m.CompanyName)
                        @Html.ValidationMessageFor(m => m.CompanyName)
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        @Html.LabelFor(m => m.CompanyPosition)
                        @Html.TextBoxFor(m => m.CompanyPosition)
                        @Html.ValidationMessageFor(m => m.CompanyPosition)
                    </div>
                </div>
                <div class="col-xs-12">
                    <div class="form-group">
                        @Html.LabelFor(m => m.CompanyBio)
                        <span class="bs-tooltip" data-toggle="tooltip" data-placement="top" title="Max 1000 characters long"><i class="fa fa-info-circle" aria-hidden="true"></i></span>
    
                        @Html.TextAreaFor(m => m.CompanyBio, new { @rows = "4", @maxlength = "1000" })
                        @Html.ValidationMessageFor(m => m.CompanyBio)
                    </div>
    
                    @TempData["Status"]
                    <div class="form-group nomargin">
    
                        <div class="text-right">
    
                            <button class="button" type="submit"><i class="fa fa-floppy-o" aria-hidden="true"></i> Update</button>
    
                        </div>
    
                    </div>
                </div>
    
            </div>
        </div>
    
    }
    

    I have everything that needs to be included (as far as I'm aware) well before the form and there are no console errors.

        <script src="/scripts/jquery.js"></script>
        <script src="/scripts/bootstrap.min.js"></script>
        <script src="/scripts/jquery-val.js"></script>
    
        <script src="/scripts/jquery.unobtrusive-ajax.min.js"></script>
    

    I have also made sure that UnobtrusiveJavaScriptEnabled is set to true in the web.config but I'm still getting a full page rendered when I post the form.

    Initially: enter image description here

    After: enter image description here enter image description here

    Feeling pretty dumbfounded that I've spent more that a couple of hours looking into this even though it's clearly working in a sort of way.

    Is it possible / a known thing for this to happen? I searched around but couldn't find any topics with a similar issue, unless I was just wording things wrong.

    Just looking for a nudge in the right direction if anybody has any ideas?

  • Matthew Southern 2 posts 72 karma points
    Aug 09, 2016 @ 20:58
    Matthew Southern
    0

    I have spent more time looking into this but still cannot find an answer. The whole page is definitely resubmitted when the partial view is returned from the surface controller which confuses me endlessly and I have recreated / tested this in a standard C# MVC application and it works with the same fundamentals that I have used here.

    Would I report this as an issue / bug on http://issues.umbraco.org/issues? or is this just an underlying problem that will have to be hacked / worked around?

  • Simon Ziegler 10 posts 81 karma points
    Mar 12, 2017 @ 10:13
    Simon Ziegler
    1

    I had the same problem for 2 days and eventually found that using jquery.unobtrusive-ajax.js works, while the minified version, jquery.unobtrusive-ajax.min.js renders the partial to a fresh empty page. I have no idea why, which troubles me.

    Update: I got a suggestion from Paul Seal (www.codeshare.co.uk - it's good, so take a look) indicating that the minified script file might be using the deprecated 'live' method rather than the 'on' method. I think that this is the case. This minified script came from Nuget, which is another concern. I have still to resolve where to find a correct minified script, however using the non minified version does the trick for now.

Please Sign in or register to post replies

Write your reply to:

Draft