0

I am trying to open PartialView inside jquery modal Popup on a link click event in my MVC, Umbraco project. The partial view loads fine however submitting the button inside modal popup calls the ActionMethod correctly but then instead of opening the modal popup in the same parent window it launches it in a new window. No sure what i am doing wrong here. Below is my code:

Partial View

  @inherits Umbraco.Web.Mvc.UmbracoViewPage<Source.Models.SchoolFindYouModel>
<script>
    $(function () {
        $("#dialog-modal-school").dialog({
            autoOpen: false,
            width: 650,
            height: 500,
            show: {
                effect: "drop",
                duration: 1000
            },
            hide: {
                effect: "drop",
                duration: 1000
            }
        });

        $("#modal-opener-school").click(function () {
            $("#dialog-modal-school").dialog("open");
        });


    });
</script>

<a href="#" id="modal-opener-school">Let Schools find you</a>

<div id="dialog-modal-school" title="Let Schools find you">

    <p>Upload your CV/Resume for free.</p>

        @using (Html.BeginForm("SchoolFindsYou", "School", FormMethod.Post,  new { enctype = "multipart/form-data" }))

        {
        <div class="editor-label">
            @Html.LabelFor(m => m.email, "Email address")

        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.email)
            @Html.ValidationMessageFor(model => model.email)
        </div>
        <div class="editor-label">
            @Html.LabelFor(m => m.password, "Password")
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.password)
            @Html.ValidationMessageFor(model => model.password)
        </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.search, "Search keyword")
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.search)
                @Html.ValidationMessageFor(model => model.search)
            </div>

            <div class="editor-label">
                @Html.LabelFor(m => m.location, "Location")
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.location)
                @Html.ValidationMessageFor(model => model.location)
            </div>

            <input type="file" name="attachment" id="attachment" />
            @Html.ValidationMessageFor(model => model.attachment)

            <br />
            <input type="submit" name="btnSchoolFindYou" value="Upload CV/Resume" id="btnSchool"/>
            <br />
            <br />
            <p>By clicking activate jobs you confirm that you agree to our <a href="/independent/TandC.aspx">terms and conditions</a> and <a href="/independent/Privacy.aspx">privacy policy</a></p>

    }
</div>
@if (!ViewData.ModelState.IsValid)
{
    <script type="text/javascript">
        $(document).ready(function () {
            $("#modal-opener-school").click();
        });

    </script>
}

PartialView Cotroller

   public class SchoolController : Umbraco.Web.Mvc.SurfaceController
    {
        [HttpGet]
        public ActionResult SchoolFindsYou()
        {
            SchoolFindYouModel s = new SchoolFindYouModel();
            return PartialView("_PartialSchoolFindYou", s);
        }

        [HttpPost]
        public ActionResult SchoolFindsYou(SchoolFindYouModel SchoolFindYou)
        {
            SchoolFindYouModel s = new SchoolFindYouModel();
            if (ModelState.IsValid)
            {
                if (Request.Files.Count > 0)
                {
                    var file = Request.Files[0];
                    if (file != null && file.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        var path = Path.Combine(Server.MapPath("~/media/cv/"), fileName);
                        file.SaveAs(path);
                    }
                }
                return PartialView("_PartialSchoolFindYou", s);
            }
            else
            {
                return PartialView("_PartialSchoolFindYou", s);
            }
        }
    }

After Postback it opens the partialView in ( instead of my parentView inside modal popup )

http://localhost:49721/umbraco/Surface/School/SchoolFindsYou
Pathan
  • 75
  • 3
  • 9
  • If you had any `@section` blocks in your view and wanted to re-use a view for a full page call or inside of a modal, you can [wrap the view in different master layout pages](https://stackoverflow.com/a/50177298/1366033) – KyleMit May 04 '18 at 14:29

1 Answers1

1

It's because you're making an http post and redirecting the request to the partial view. You need to return the parent View that contains this partial view instead and your partial view will be rendered inside that parent view.

So instead of return PartialView("_PartialSchoolFindYou", s); in your controller, you should return View(s);, and inside of that view somewhere you should have

@Html.Partial("_PartialSchoolFindYou", s);

A. Burak Erbora
  • 1,054
  • 2
  • 12
  • 26
  • If I call parent view then it through exception by entering a loop. – Pathan Oct 19 '15 at 21:17
  • What do you mean? how do you call this partial inside your parent? – A. Burak Erbora Oct 19 '15 at 21:19
  • Below is my Parent view: @inherits Umbraco.Web.Mvc.UmbracoViewPage @{ Layout = "Master.cshtml"; }
    @Html.Action("SchoolFindsYou", "School")
    – Pathan Oct 19 '15 at 21:21
  • Ok, you can use `@Html.Partial` (which will only render the partial) instead of `@Html.Action` (which will actually *call* the action method on the controller). That is why you're getting the infinite loop. – A. Burak Erbora Oct 19 '15 at 21:23
  • Thanks. Got that issue solved but now i have two BeginForm in my view and for some reason boths validation is getting fired one i press submit button on one only. any idea please? – Pathan Oct 19 '15 at 21:54
  • that is really another question, but try using strongly typed models in your view, so only your model gets validated. – A. Burak Erbora Oct 20 '15 at 06:13