1

我有一些代码显示一个带有数据库条目列表的保管箱,并在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);
    }
4

1 回答 1

1

不要在 onchange 上做 ajax,而是在下面做

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

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

于 2013-06-29T10:57:26.010 回答