2

I am trying to load the contents of the modal using AJAX. Here is the code snippet what I am trying to do.

Index.cshtml

<button type="submit" class="btn btn-default" onclick="Create()">Create</button>

index.js

function AddTask() {
   $Modal = $('#myModal')
   $.get('/Customer/Create', function (data) {
      $Modal.modal({ show: true });
   });
}

Customer Controller

public ActionResult Create()
{
   return PartialView();
}

Create.cshtml

<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">×</button>
        <h3>Modal header</h3>
    </div>
    <div class="modal-body">
        <p>My modal content here…</p>
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal">Close</button>
    </div>
</div>

How can I load the Create.cshtml contents in the modal?

2
  • Thank you for your input but didn't work. It didn't work as a modal. Commented Dec 1, 2015 at 22:53
  • you have Create() in the onclick event on your button, but the function you want to call is named AddTask - is that a typo in your question, or is that actually your code? Commented Dec 2, 2015 at 3:26

2 Answers 2

1

I believe the following should work for you, although, I'm not sure about the type="submit"

<button type="submit" href="@Url.Action("Create","Customer")"
  class="btn btn-default" data-toggle="modal" data-target="#myModal">Create</button>

or

<button type="submit" data-remote="@Url.Action("Create","Customer")"
  class="btn btn-default" data-toggle="modal" data-target="#myModal">Create</button>

Although, this has been depreciated, so you'll need to do it manually in version 4 (or write your own extension to handle this like it did in 3.x)

I believe you'll also need to remove <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog"> and the last </div> from Create.cshtml, as these belong in your parent page (Index.cshtml).

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

Comments

1

You can do like this. Suppose We get partial view for modal content.Instead of get method

function AddTask() {
   $Modal = $('#myModal');
   $Modal.load('/Customer/Create');
   $Modal.modal('show');
}

Note: Here i used bootstrap 2.3.2.I hope you can get idea of this.

Comments

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.