【问题标题】:ASP.NET MVC partial view does not call my Action when i used $.getJSON method当我使用 $.getJSON 方法时,ASP.NET MVC 部分视图不会调用我的操作
【发布时间】:2012-07-16 07:49:21
【问题描述】:

I am building a Website for document management system on ASP.NET MVC3, in a page I am using a Partial view, the Partial View represents a simple Form.There is a drop down list and when one option is selected the ID选择的选项应该去行动结果。为此,我使用 $.getJSON 方法将值传递给操作方法。但问题是 ID 没有转到 ActionResult。谁能帮我解决这个问题?

cshtml代码

<div >
  @using (Html.BeginForm("GetFilteredActivityLogs", "Document", FormMethod.Post, new
    {
        id = "activityLog",
        @class = "form-horizontal",

    }))
  {
<h3>Activity Log</h3>
   <div>
    <div style="float: left" class="control-label">
        <label>
            Action Type
        </label>
    </div>
    <div class="controls">
    @Html.DropDownListFor(model => model.SelectedActionId, Model.ActionTypes as SelectList, "All", new { 
        id = "SelectedActionId" 
})
    </div>
    <table class="table table-bordered" style="margin-top: 20px;  width: 100%;">
        <thead>
            <tr>
                <th style="width: 15%; text-align: center">
                    Employee No

                </th>
                <th style="width: 20%; text-align: center">
                    Employee Name
                </th>
                <th style="width: 45%; text-align: center">
                    Action
                </th>
                <th style="width: 20%; text-align: center">
                    Date and Time
                </th>
            </tr>
        </thead>
        <tbody id="Activities">
        </tbody>
    </table>
    </div>
  }
</div>

控制器:

 public ActionResult GetFilteredActivityLogs(int actionTypeId)
    {
        return View();
    }

脚本:

<script type="text/javascript">
    $(function ()
    {
        $('#SelectedActionId').change(function ()
        {
            var selectedActivityId = $(this).val();

            $.getJSON('@Url.Action("GetFilteredActivityLogs", "Document")', { activityId: selectedActivityId }, function (FilteredActivities)
            {
                var ActivitySelect = $('#Activities');
                // GroupSelect.empty();
                $.each(FilteredActivities, function (index, activity)
                {
                    // some code goes here...
                });
            });
        });
    });
</script>

【问题讨论】:

  • 尝试将您的 json 从 { activityId: selectedActivityId } 更改为 { actionTypeId: selectedActivityId }。您的视图需要一个传递的参数,但您没有发送具有该名称的参数。
  • 感谢 BuildStarted 。当我改变我的json时它起作用了。再次感谢您.....

标签: jquery asp.net-mvc-3


【解决方案1】:

正如 BuildStarted 所说,我认为您需要使您在 getJSON 请求(“activityId”)中发送的数据密钥与您的操作参数相匹配。当有一个参数时,我经常只使用“id”,以便在默认路由上获得匹配,从而获得一个不错的 URL(即 /Document/GetFilteredActivityLogs/123)。

这里还有一个观察:我认为值得一看 jQuery 表单插件 (http://malsup.com/jquery/form/)。就目前而言,您定义了两次参数,一次在 BeginForm 中,另一次在您的 javascript 中。如果您使用插件在表单本身上启用 ajax,您只能在 BeginForm 中定义参数,然后在“更改”事件处理程序中提交表单。

【讨论】:

    猜你喜欢
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多