我有一个带有级联下拉列表的局部视图的视图。
主视图:
@model Production.ViewModels.SubmissionVM
@{
ViewBag.Title = "Create";
}
<h3>Create</h3>
@using (Html.BeginForm("Create", "Submissions", FormMethod.Post, new { id = "SubmissionForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Submission</legend>
<div id="planning" data-link="@(ViewBag.FormLink)">
<table>
<thead>
<tr>
<th>@Html.LabelFor(model => model.PlanningId)</th>
<th>@Html.Label("Quality")</th>
</tr>
</thead>
<tbody>
<tr>
<td>@Html.DropDownList("PlanningId", (SelectList) ViewBag.Plannings, string.Empty)</td>
<td>
<select id="Quality">
<option></option>
<option>Good</option>
<option>MRB/Reject</option>
</select>
</td>
</tr>
</tbody>
</table>
</div>
<div id="workorder">
@Html.Partial("LoadForm");
</div>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/Scripts/SubmissionScript.js")
}
我在<div id="workorder"></div>
内部有部分视图,根据<div id="planning"></div>
内的下拉值,加载不同的视图。
部分视图:
@model Production.ViewModels.SubmissionVM
<h4>Part Info</h4>
<table id="workorder-table">
<thead>
<tr>
<th>@Html.LabelFor(model => model.WorkorderId)</th>
<th>@Html.LabelFor(model => model.SubmissionDate)</th>
<th>@Html.LabelFor(model => model.PartId)</th>
<th>@Html.LabelFor(model => model.ProductionTypeId)</th>
</tr>
</thead>
<tbody>
<tr>
<td>@Html.DisplayFor(model => model.Workorder) @Html.HiddenFor(model => model.WorkorderId)</td>
<td>@Html.TextBoxFor(model => model.SubmissionDate, "{0:MM/dd/yyyy}", new { @class = "datefield" })</td>
@if (@ViewBag.Quality == "Good")
{
<td>@Html.DisplayFor(model => model.Part) @Html.HiddenFor(model => model.PartId)</td>
<td>@Html.DisplayFor(model => model.ProductionType) @Html.HiddenFor(model => model.ProductionTypeId)</td>
}
else
{
<td>@Html.DropDownList("PartId", (SelectList)ViewBag.Parts, string.Empty, new { @data_link = (string)@ViewBag.PartLink })</td>
<td>@Html.DropDownList("ProductionTypeId", (SelectList)ViewBag.ProductionTypes, string.Empty)</td>
}
</tr>
</tbody>
</table>
JS文件:
$(function () {
$('#PartId').change(function () {
var partId = $(this).val();
alert("PartId");
$('#ProductionTypeId').empty();
$('#ProductionTypeId').prop('selectedIndex', 0);
if (partId > 0) {
url = $('#ProductionTypeId').data('link');
// AJAX call that re-populate ProductionType dropdown depending on the Part selected.
$.ajax({
type: 'POST',
url: url,
dataType: 'json',
data: { partId: partId },
success: function (codes) {
$('#ProductionTypeId').append('<option value=""></option>');
$.each(codes, function (i) {
$('#ProductionTypeId').append('<option value = "' + codes[i].Value + '">' + codes[i].Text + '</option>');
});
},
error: function (ex) {
$('#ProductionTypeId').append('<option value=""></option>');
}
}); // END $.ajax()
} // END if(partId > 0)
}); // END $('#Part').change()
$('#Quality').change(function () {
LoadForm();
});
$('#PlanningId').change(function () {
LoadForm();
});
function LoadForm() {
var planningId = $('#PlanningId').val();
var quality = $('#Quality').val();
if (planningId > 0 && quality != '') {
var url = $('#planning').data('link');
var queryString = url + '?planningId=' + planningId + '&quality=' + quality;
$('#workorder').load(queryString);
$('#workorder').show();
}
}
});
部分视图正确呈现。它根据下拉列表显示不同的视图。但是当我选择一个零件时,ProductionType不会重新填充。我知道它不会执行JS文件,因为它不显示警报。我在主视图中包含JS文件(SubmissionScript.js)。我试图将它包含在部分视图中,但它没有做任何事情。
我做错了什么?