我的json代码没有发布控制器的特定操作 [英] My json code is not posting a specific action of a controller

查看:74
本文介绍了我的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>

推荐答案

(文件).ready(function(){
(document).ready(function () {


('#btnClick')。click(function(){
('#btnClick').click(function () {


.ajax({
url:'/ Customers / IndexPartial',
dataType:'html',
contentType:'application / html',
type:'post',
success:function(data){
.ajax({ url: '/Customers/IndexPartial', dataType: 'html', contentType: 'application/html', type: 'Post', success: function (data) {


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆