本文介绍了我的json代码没有发布控制器的特定操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个IndexPartial,它是一个部分视图,它包含一个表,该表应该显示来自Customer Model Class的simiple表。我还有另一个Main页面,它应该将IndexPartial View发布到按钮点击动作的主视图中,但它不是。



我尝试过:



I have a IndexPartial which is a Partial View and it contains a table which should show a simiple Table from Customer Model Class. I also have another Main page which should Post the IndexPartial View into the Main View on the button click action but it isnt.

What I have tried:

My Main.cshtml code

<pre>@model PartialViews.Models.Customer

@{
    ViewBag.Title = "Main";
}

<body>

    <input type="button" value="Click" id="btnClick" />
    <div class="container">
        <h2>Customer's List</h2>
        <div id="dvCustomerDetails" style="width: 50%; height: 130px; display: none">
        </div>


        @*@{
            Html.RenderAction("Index", "Customers");
            <div>@Html.Partial("IndexPartial")</div>}*@
    </div>

    <div id="output">

    </div>


</body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


<script type="text/javascript">
    $(document).ready(function () {
        $('#btnClick').click(function () {

            $.ajax({
                url: '/Customers/IndexPartial',
                dataType: 'html',
                contentType: 'application/html',
                type: 'Post',
                success: function (data) {
                    $('#dvCustomerDetails').html(data);
                }
            });

            if (document.getElementById("tableCount")) {

                var rowCount = $('#tableCount tr').length;
                var totalRow = rowCount - 1;
                document.getElementById('output').innerHTML = totalRow;
            } else {
                alert("Table doesn't exists yet");
            }
        });
    });
</script>





我的CustomersController的IndexPartial Action





My CustomersController's IndexPartial Action

[HttpPost]
public ActionResult IndexPartial()
{
    AdventureWorksLT2012Entities adv = new AdventureWorksLT2012Entities();

    return PartialView("IndexPartial", adv.Customers.ToList());

}







我的IndexPartial.cshtml






My IndexPartial.cshtml

@using System.Runtime.InteropServices.ComTypes
@using PartialViews.Models
@model IList<PartialViews.Models.Customer>

<table class="table table-responsive">
    <tr>
        <td>
            Title
        </td>
        <td>
            Firstname
        </td>
        <td>
            Lastname
        </td>
        <td>
            Email
        </td>
        <td>
            Phone
        </td>
        <td>
            Date
        </td>
    </tr>
    
    @foreach (var item in Model)
    {
        <tr>
            @item.Title
        </tr><tr>
            @item.FirstName
        </tr><tr>
            @item.LastName
        </tr><tr>
            @item.EmailAddress
        </tr><tr>
            @item.ModifiedDate
        </tr>
    }
</table>

推荐答案




这篇关于我的json代码没有发布控制器的特定操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:41