0

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: When the page loads and the form shows

After: When the form is submitted and the correct partial view is returned but inside of an entire

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?

  • where is your html element located in which you replace html? – Ehsan Sajjad Aug 08 '16 at 19:15
  • It's located inside of a template that inherits Umbraco.Web.Mvc.UmbracoTemplatePage. That's really the only special thing about the div, does that help in anyway? – Matthew Southern Aug 08 '16 at 19:26
  • is your ajax form view loaded in that div? – Ehsan Sajjad Aug 08 '16 at 19:32
  • Yeah it is, and the partial view that I want does get loaded into that div but along with the rest of the site as though it has a layout but I specifically tried putting layout = null before but what is currently happening still happened. – Matthew Southern Aug 08 '16 at 19:42

1 Answers1

0

Umbraco can be funny with partials. Try returning CurrentUmbracoPage() in the controller.

As for the message, you could use TempData as it only lasts for one request, rather than the model.

BezzyBloke
  • 54
  • 8
  • Returning CurrentUmbracoPage() wouldn't work as I need to only update a small amount of the page to keep the currently active div open. With what experience I have, this feels like it should be the way to do this and using tempdata wouldn't be as practical as passing through a model and returning a partial with the result and saved / unsaved information. Can you remember anywhere that people has discussed partials acting funny in umbraco or is that just from your personal experiences? – Matthew Southern Aug 08 '16 at 22:06
  • Funnily enough I just gave returning CurrentUmbracoPage() a go anyway and had scarily similar results. I have the feeling that somehow a layout is getting outputted with my partial which would both make sense and no sense at all as how could I even control or remove a layout if one was being added. – Matthew Southern Aug 08 '16 at 22:09
  • Partials acting strange is from personal experience unfortunately. As you say, it does sound as if a view is getting rendered in the background too. If it comes down to it, I'd use JS to call an UmbracoApiController through Ajax and return an object which will contain that message. Apologies for being unable to solve this. – BezzyBloke Aug 09 '16 at 07:46
  • That would probably be the best option if I wanted to just update forms with these partials (I had something along those lines working earlier.) but I have been hoping to do some more complex things with them later on and I really want to see this working the way that it should. – Matthew Southern Aug 09 '16 at 19:19