Facing issue while rendering the Partial view usin

2019-07-20 15:40发布

问题:

I have an Area like below.

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 ?

回答1:

  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");


回答2:

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);
            }
        });
    });
});


回答3:

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.



回答4:

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" />


回答5:

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>