FormViewCity 为mvc控制器传给view的数据,包括一个MyCitys集合字段.
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/Main.Master" Inherits="System.Web.Mvc.ViewPage<MvcStudyStep.Controllers.FormViewCity>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentMain" runat="server">
<script type="text/javascript">
var b=<%= Model.MyCitys.Count %>;
var arrayCity = new Array(b);
<%
for(int i = 0; i < Model.MyCitys.Count; i++)
{
%>
arrayCity[<%=i%>] = '<%= Model.MyCitys[i].Code+"#"+Model.MyCitys[i].PCode+"#"+Model.MyCitys[i].Name %>';
<%
}
%>
</script>
<form action="/City/Save" method="post">
<select name="myCity" onchange="javascript:CityChange();">
<option></option>
<%
var pCity = from p in Model.MyCitys
where p.PCode == ""
select p;
foreach(var o in pCity)
{ %>
<option value="<%= o.Code%>"><%= o.Name%></option>
<%} %>
</select>
<div id="divCityChild">
<select name="myCityChild"></select>
</div>
<input id="btnSave" type="submit" value="保存" />
</form>
<script type ="text/javascript">
function CityChange() {
var str = "<select name='myCityChild'>";
str += "<option></option>";
var pCity = document.all.myCity.value;
for (var i = 0; i < arrayCity.length; i++) {
var arrayTemp = arrayCity[i].split('#');
if (arrayTemp[1] == pCity) {
str += "<option value='"+ arrayTemp[0] +"'>";
str += arrayTemp[2];
str += "</option>";
}
}
str += "</select>";
//alert(str);
document.all.divCityChild.innerHTML = str;
}
</script>
</asp:Content>