MVC3 ajax返回新视图

时间:2013-06-29 10:23:56

标签: asp.net-mvc-3 jquery

我有一些代码显示一个包含数据库条目列表的Dropbox,并在change事件上调用控制器操作。控制器将所选条目作为字符串并返回一个新视图(我认为这是我的问题),问题是我认为旧视图仍然存在,因为新返回的视图永远不会显示。

我是否需要重新设计,如果不是,我是否应该强行摧毁任何旧观点?

我的代码如下:

EditSchool查看:

@model namespace.Models.SchoolDetails
@{
    ViewBag.Title = "EditSchool";
    Layout = "~/Views/AuthorisedAdmin/_LayoutAdmin.cshtml";
}

<script src="@Url.Content("~/Scripts/chosen/chosen.jquery.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/bubble-popup-chosen-upload-functions.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $("#School").change(function () {
            var SelectedSchool = $('#School').val();
            $.ajax({
                url: '/AuthorisedAdmin/RetrieveSchool'
                , type: 'POST'
                , data: { School: SelectedSchool }
                , success: function (data) {
                }
                , error: function (request, textStatus, errorThrown) {
                    alert(errorThrown.toString());
                }
                , complete: function (request, textStatus) {
                }
            });
        });
    });
</script>

<fieldset>
    <legend>Select School</legend>
    <div class="editor-field">
        @Html.DropDownList("School", ViewBag.RegisteredSchools as SelectList, namespace.Models.Helpers.LanguageSchoolsConstants.m_sDropdownDisplayText,
            new
            {
                id = "School",
                @class = "chosen",
            })
    </div>
</fieldset>

@{
    if (null != Model)
    {
        @Html.Partial("Partial/EditSchoolPartial", Model)
    }
    else
    {
        @Html.Partial("Partial/NoSchoolSelected")
    }    
}

请注意,部分视图(EditSchoolPartial)可能不是这里的问题,因此我不会发布代码。

感兴趣的控制器方法:

EditSchool行动:

public ActionResult EditSchool()
{
    List<string>kRegisteredSchools = DBHelperFunctionsSchool.Instance().GetRegisteredSchoolsNamesOnly();
    ViewBag.RegisteredSchools = new SelectList(kRegisteredSchools, "Name");
    SchoolDetails schoolDetails = null;//DBHelperFunctionsSchool.Instance().GetSchoolDetailsForName(kRegisteredSchools.FirstOrDefault());
    return View(schoolDetails);
}

RetrieveSchool动作(由AJAX调用):

[HttpPost]
    public ActionResult RetrieveSchool(string School)
    {
        SchoolDetails schoolDetails = null;
        List<string> kRegisteredSchools = DBHelperFunctionsSchool.Instance().GetRegisteredSchoolsNamesOnly();
        ViewBag.RegisteredSchools = new SelectList(kRegisteredSchools, "Name");

        try
        {
            schoolDetails = new SchoolDetails();
            schoolDetails.School = DBHelperFunctionsSchool.Instance().GetSchoolForName(School);
            DBHelperFunctionsSchool.Instance().PopulateSchoolDetailsSuppData(schoolDetails);
            schoolDetails.ActionNameToExecuteOnFormSubmit = "EditSchoolDetails";
            schoolDetails.ControllerNameToExecuteOnFormSubmit = "AuthorisedAdmin";
        }
        catch
        {
            schoolDetails = null;
        }
        finally
        {
        }

        return View("EditSchool", schoolDetails);
    }

1 个答案:

答案 0 :(得分:1)

不要在onchange上执行ajax,而是在

下面执行
 $("#School").change(function () {
       var SelectedSchool = $('#School').val();
       window.location='/AuthorisedAdmin/RetrieveSchool?School='+SelectedSchool;
  });

注意:您可能必须提供完整路径网址而不是相对网址。