当我使用$ .getJSON方法时,ASP.NET MVC局部视图不会调用我的Action

时间:2022-11-30 17:33:37

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 of selected option should go to action Result. To do that i used $.getJSON method to pass values to action method. but problem is ID not goes to ActionResult. Can anyone help me to solve this problem?

我正在ASP.NET MVC3上构建一个文档管理系统的网站,在我正在使用部分视图的页面中,部分视图代表一个简单的Form.There有一个下拉列表,当选择一个选项时,所选选项的ID应该去行动结果。为此,我使用$ .getJSON方法将值传递给action方法。但问题是ID不会转到ActionResult。任何人都可以帮我解决这个问题吗?

cshtml code

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

controller:

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

Script:

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

1 个解决方案

#1


1  

As BuildStarted says I think you need to make the data key you're sending in the getJSON request ("activityId") match the parameter on your action. I often just use "id" when there's a single parameter in order to get a match on the default route and therefore a nice URL (ie /Document/GetFilteredActivityLogs/123).

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

One other observation here: I think it would be worth having a look at the jQuery form plugin (http://malsup.com/jquery/form/). As it stands you're defining your parameters twice, once in BeginForm and again in your javascript. If you enable ajax on the form itself using the plugin you can define the params only in BeginForm, then in the "change" event handler submit the form.

另一个观察:我认为值得看一下jQuery表单插件(http://malsup.com/jquery/form/)。现在你要定义你的参数两次,一次在BeginForm中,再次在你的javascript中。如果使用插件在表单上启用ajax,则只能在BeginForm中定义params,然后在“change”事件处理程序中提交表单。

#1


1  

As BuildStarted says I think you need to make the data key you're sending in the getJSON request ("activityId") match the parameter on your action. I often just use "id" when there's a single parameter in order to get a match on the default route and therefore a nice URL (ie /Document/GetFilteredActivityLogs/123).

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

One other observation here: I think it would be worth having a look at the jQuery form plugin (http://malsup.com/jquery/form/). As it stands you're defining your parameters twice, once in BeginForm and again in your javascript. If you enable ajax on the form itself using the plugin you can define the params only in BeginForm, then in the "change" event handler submit the form.

另一个观察:我认为值得看一下jQuery表单插件(http://malsup.com/jquery/form/)。现在你要定义你的参数两次,一次在BeginForm中,再次在你的javascript中。如果使用插件在表单上启用ajax,则只能在BeginForm中定义params,然后在“change”事件处理程序中提交表单。