部分视图中的Javascript和Ajax?

时间:2012-03-25 18:00:22

标签: javascript jquery asp.net-mvc-3

我有一个包含JavaScript和Ajax的局部视图,但在按下创建按钮时不运行JavaScript代码

我的局部视图(“_Campus”),ava脚本代码我放在局部视图中,而不是在父视图中:

@model RolesMVC3.Areas.Superusuario.Models.CampusViewModel
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
           <script type="text/javascript">
               $(document).ready(function () {
                   $("#University").change(function () {
                       var idDept = $(this).val();
                       $.getJSON("Office/GetCityList", { id: idDept },
                function (myData) {
                    var select = $("#city");
                    select.empty();
                    select.append($('<option/>', {
                        value: 0,
                        text: "-- select city --"
                    }));
                    $.each(myData, function (index, itemData) {
                        select.append($('<option/>', {
                            value: itemData.Value,
                            text: itemData.Text
                        }));
                    });
                });
                   });
               });
</script>

  @using (Html.BeginForm())
  {
       @Html.DropDownListFor(model => model.IdUniversity,
                   new SelectList(ViewBag.IdUniversity as System.Collections.IEnumerable, "IdUniversity", "Name"),
        "-- select university --", new { id = "University" })

        @Html.DropDownListFor(model => model.City,
        new SelectList(Enumerable.Empty<SelectListItem>(), "IdCity", "NameCity"), "-- select city --",
        new { id = "city" })
  }

父视图:

@model RolesMVC3.Models.OFFICE

@{
    ViewBag.Title = "Create";
}

<h2>OFFICE</h2>

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


@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>OFFICE</legend>

        <fieldset>
         @Html.Partial("_Campus")
        </fieldset>


        <div class="editor-label">
            @Html.LabelFor(model => model.AddressOffice)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AddressOffice)
            @Html.ValidationMessageFor(model => model.AddressOffice)
        </div>



        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

如何运行JavaScript代码?

1 个答案:

答案 0 :(得分:0)

检查json是否以您期望的格式返回

$.each(myData, function (index, itemData) {
                        select.append($('<option/>', {
                            value: itemData.Value,
                            text: itemData.Text
                        }));

尝试这可能

$.each(myData, function (index, itemData) {
                        select.append($('<option/>', {
                            value: index,
                            text: itemData 
                        }));