简体   繁体   English

jQuery未调用控制器方法-MVC

[英]Controller method is not called by jQuery - MVC

I am trying to call a controller method through jQuery, jQuery functions is called but the its not calling the controller method. 我正在尝试通过jQuery调用控制器方法,已调用jQuery函数,但其​​未调用控制器方法。 here is my jQuery Code: 这是我的jQuery代码:

@{
  int year = DateTime.Today.Year;
}

    <script>
        var year = '@year';
        var url = '@Url.Action("Details", "Holidays")'; 
        var updateHolidays = function() {
            $('#year').text(year);
            $('#details').load(url, { id: year });
        } 
        $('#previous').click(function() {
            year--;
            updateHolidays();
        });
        $('#next').click(function () {
            year++;
            updateHolidays();
        });
    </script>

    @if (Session["LoggedUserName"] != null)
    {
        <table>
            <tr style="height:10px">
                <td>
                    <h2>Holiday Calandar</h2>
                </td>
                <td>
                    <input type="image" id="previous" value="submit" src="~/Images/left-arrow.png" style="width:30px" />
                </td>
                <td>
                    <h2>@year</h2>
                </td>
                <td>
                    <input type="image" id="next" value="submit" src="~/Images/right-arrow.png" style="width:30px"  />
                </td>
                <td>
                        <ul id="menu">
                        <li>@Html.ActionLink("Add Holiday", "Create", null, new { id = "lnkCreate" })</li>
                        </ul>
                </td>

            </tr>


        </table>
    }

and this is my controller method code: 这是我的控制器方法代码:

public ActionResult Details(int year)
        {
            var model = db.tbl_HolidayList.Where(h => Convert.ToDateTime(h.Holiday_date).Year == year);
            return PartialView(model);
        }

I tried to debug but its not going into detail method. 我尝试调试,但是没有详细介绍方法。 can anybody tell where I am mistaking?? 有人能告诉我我在哪里吗?

Instead of this 代替这个

$('#details').load(url, { id: year });

try 尝试

$('#details').load(url, { year: year });

Since the name of your parameter in the action is Details(int year) 由于操作中参数的名称为Details(int year)

The other issue is you need an element with id=details: 另一个问题是您需要一个id = details的元素:

<div id='details'></div>

Because .load will not fire if the selector finds no elements. 因为如果选择器未找到任何元素, .load不会触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM