2

I have an Area like below.

enter image description here

Below are the Controller actions.

[HttpGet]
public ActionResult Index_partial()
{
    return PartialView("_PartialPage1");
}

[HttpGet]
public ActionResult Index()
{
    AdminModule model = new AdminModule();
    model.MyName = "My Name";
    return View("Index", model);
}

View

@model _1.Areas.Admin.Models.AdminModule
@{
    ViewBag.Title = "Index";
    Layout = "~/Areas/Admin/Views/Shared/_LayoutPage1.cshtml";
}
<h2>
    Index</h2>
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript">
</script>
<script type="text/javascript" src="/scripts/jquery.unobtrusive-ajax.js">
</script>
<div id="myForm">
    @using (Html.BeginForm())
    {
        <p id="pid">
        </p>
        <input onclick="ClickHere();" type="submit" value="Button" />
    }
    <script language="javascript" type="text/javascript">
        function ClickHere() {
        debugger;
            var url = '@Url.Action("Index_partial", "Admin")';
            $('#p').load("@Url.Action("Index_partial", "Admin")");
        }

    </script>
</div>

===================================

Partial View

@model _1.Areas.Admin.Models.AdminModule
@using (Ajax.BeginForm("Index", "Admin", 
             new AjaxOptions { UpdateTargetId = "myForm", HttpMethod = "Post" }))
{
    @Html.LabelFor(i => i.MyName)
    @Html.TextBoxFor(i => i.MyName)
    @Html.ValidationMessageFor(i => i.MyName)
    <p id="getDateTimeString">
    </p>
    <input type="submit" value="Click here" id="btn" />
}

=====================================

In the View, I have a button, on clicking it should render the Partial View in

<p id="pid"></p>

Issue is - on clicking the button is navigating the page to Partial View, It should instead render the Partial View Html in p tag. How to do this ?

2
  • 1
    at the first glance i guess you need to cancel the submit event but returning false in your javascript function Commented Feb 6, 2013 at 14:07
  • 1
    @SMC: You can answer your own question instead of modifying an existing answer. :) Commented Feb 6, 2013 at 14:30

5 Answers 5

2
  1. Remove the @using (Html.BeginForm())
  2. Add an id to your button

:

< input onclick="ClickHere();" type="submit" id="Button" value="Button" />
  1. reassign your click event to unobtrusive rather than in-line:

:

$('#Button').click(function(){
       debugger;
            var url = '@Url.Action("Index_partial", "Admin")';
            $('#p').load("@Url.Action("Index_partial", "Admin")");
        }
  1. change your return partial

:

return PartialView("_PartialPage1");
Sign up to request clarification or add additional context in comments.

Comments

1

Remove @using (Html.BeginForm()) and add this bit of HTML:

<p id="pid"></p>

<button type="button" id="mybtn">Load partial view</button>

and then, add a bit o jQuery:

(function() {
    $('#mybtn').click(function() {
        $.ajax({
            url: '@Url.Action("Index_partial", "Admin")',
            type: 'GET',
            cache: false,
            success: function(result) {
                $('#pid').empty().html(result);
            }
        });
    });
});

4 Comments

This is not impacting my code public PartialViewResult Index_partial()
This is nore required @using (Html.BeginForm()).
Sure... you can remove that.
Finally i should remove the script and button from <div id="myForm">
0

Controller:

[HttpGet]
public ActionResult Index_partial()
{
    return PartialView("_PartialPage1");
}

Inside the view/javascript.

<script language="javascript" type="text/javascript">
    function ClickHere() {
        $.ajax({
        url: "@Url.Action(MVC.Admin.Index_partial())",
        async: false,
        data: { },
        success: function(data) {
            $("#pid").empty();
            $("#pid").html(data);
        }
    });
    }
</script>

I use T4MVC to generate the url.

Hope this helps.

Comments

0

Home Controller:

[HttpGet]
public ActionResult PartialView()
{
   return this.PartialView("PartialView");
}

View

<script type="text/javascript">

    $(document).ready(function () {

        $("#btnSubmit").click(function() {

            $("#renderedHtml").load('Home/PartialView');

        });

    });

</script>


    <div id="renderedHtml"></div>
    <input type="submit" value="Get Partial" id="btnSubmit" />

Comments

0

View should be like this...

<div id="myForm">
    <p id="pid">
    </p>
</div>
<input id="BTN" type="submit" value="Button" />
<script language="javascript" type="text/javascript">
    $('#BTN').click(function(){
        $('#pid').load("@Url.Action("Index_partial", "Admin")");
    });
</script>

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.