我希望有人可以幫助我解決以下問題,這可能是非常基本的,但我仍然在學習MVC 3.0(和一般編碼)。級聯DropDownLists - 值不能爲空。參數名稱:字符串錯誤
我收到值不能爲空。參數名稱:字符串當我嘗試將數據加載到我的城市DropDownList時,基於狀態DropDownList中的值發生錯誤。
我希望我只是缺少一些基本的東西。 int id = int.Parse(state_id)內線JsonResult getCity調用正在拋出錯誤。
這裏是我的控制器:
public ActionResult Create()
{
ViewBag.sessionName = HttpContext.Session["SPCompanyName"].ToString();
var compID = HttpContext.Session["SPCompanyAccountID"].ToString();
ViewBag.companyID = compID;
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem { Text = "Select State", Value = "Default State" });
var states = (from c in simpleDB.simpleState select c).ToArray();
for (int i = 0; i < states.Length; i++)
{
list.Add(new SelectListItem { Text = states[i].stateFull, Value = states[i].Id.ToString() });
}
ViewData["states"] = list;//data for DropdownList State
List<SelectListItem> list1 = new List<SelectListItem>();
list1.Add(new SelectListItem { Text = "Select City", Value = "Default City" });
ViewData["cities"] = list1;//data for Dropdownlist City
return View();
}
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult getCity(string state_id)
{
int id = int.Parse(state_id);
var myData = (from m in simpleDB.simpleCity where m.simpleStateId == id select new { text = m.cityFull, value = m.Id });
return this.Json(myData, JsonRequestBehavior.AllowGet);
}
這裏是我查看全碼:
@using (Html.BeginForm(FormMethod.Post))
{
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#StateId').change(function() {
var state_id = $(this).val(); //get State ID when select value change
$.getJSON("/SPServiceLocation/getCity/" + state_id, {},
function (myData) {
var options = '';
for (var i = 0; i < myData.length; i++) {
options += '<option value="' + myData[i].value + '">' + myData[i].text + '</option>';
}
$("#CityId").html();
$("#CityId").html(options);
});
});
});
</script>
@Html.ValidationSummary(true)
<fieldset>
<legend>Step 4: Service Areas</legend>
@Html.HiddenFor(model => model.SPCompanyAccountID, new { @Value = ViewBag.companyID })
<div class="editor-label">
@Html.LabelFor(model => model.state)
</div>
<div class="editor-field">
@Html.DropDownList("state", (IEnumerable<SelectListItem>)ViewData["states"], new { id = "StateId", @class = "chzn-select" })
@Html.ValidationMessageFor(model => model.state)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.city)
</div>
<div class="editor-field" id="SP_SelectCity">
@Html.DropDownList("city", (IEnumerable<SelectListItem>)ViewData["cities"], new { id = "CityId", @class = "chzn-select" })
@Html.ValidationMessageFor(model => model.city)
</div>
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
我已經看了這個控制器和視圖代碼現在幾個小時,只是不知道問題可能是什麼。謝謝您的幫助。
把警報在您的JS之後您將STATE_ID變量 - 警報(STATE_ID),並確保你實際上是抓住了價值。此外,如果你沒有一個路徑設置爲/ SPServiceLocation/getCity/STATEID,您將需要使用傳遞參數(/ SPServiceLocation/getCity STATE_ID = MD?) – Tommy 2012-07-16 17:48:19
通過我的教程[1]正常的方法:HTTP: //www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc [2]:HTTP:// blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx – RickAndMSFT 2012-07-16 18:41:30