【发布时间】:2016-06-08 04:49:01
【问题描述】:
我的最终目标是能够在一个 DropDownList 中选择一个值,并且在做出该选择之后,我希望根据在第一个中所做的选择来填充另一个 DropDownList。我的表单应该有一个包含省份列表的 DropDownList 和一个包含所选省份城市的 DropDownList。
我是 ASP MVC 的新手,所以我不确定这将如何完成。如果这必须在 Winforms 或 WPF 中完成,我将能够实现它,因为我知道数据如何从我的业务逻辑传播到显示给用户的控件 - 但是在 MVC 中,我不知道如何做到这一点正确有效。我也在学习 javascript 和相关帮助程序(例如 jQuery),所以我需要一些帮助来使用 ajax 之类的东西来实现我的目标。
这是我已经拥有的以及我知道实现目标应该拥有的:
我的模型(捕获用户的输入):
public class CaptureCreateTrip
{
[Required]
[Display(Name = "Trip ID")]
public string TripID { get; set; }
[Required]
public string StartPointProvince { get; set; }
[Required]
public string StartPointCity { get; set; }
[Required]
public string EndPointProvince { get; set; }
[Required]
public string EndPointCity { get; set; }
}
我的表格:
请记住,我只是插入 ViewBag 引用以充当占位符,直到我可以用动态数据替换它。
@using (Html.BeginForm("CreateOneWayTrip", "Trips"))
{
@Html.ValidationSummary(false);
<fieldset>
<legend>Enter Your Trip Details</legend>
<label>Start Point</label>
@Html.DropDownListFor(m => m.StartPointProvince, (SelectList)ViewBag.Provinces);
@Html.DropDownListFor(m => m.StartPointCity, (SelectList)ViewBag.Cities);
<label>End Point</label>
@Html.DropDownListFor(m => m.EndPointProvince, (SelectList)ViewBag.Provinces);
@Html.DropDownListFor(m => m.EndPointCity, (SelectList)ViewBag.Cities);
<p style="float: none; text-align: center;">
<button type="submit" value="Create" class="btn btn-info btn-circle btn-lg">
<i class="fa fa-check"></i>
</button>
<button type="submit" value="Create" class="btn btn-warning btn-circle btn-lg">
<i class="fa fa-times"></i>
</button>
</p>
</fieldset>
}
控制器:
//To Show Create Page
public ActionResult Create()
{
return View();
}
// To Get Data after post
[HttpPost]
public ActionResult Create(Models.CaptureCreateTrip trip)
{
Models.CaptureCreateTrip t = trip;
return Redirect("~/Trips/Index");
}
//Will I need a controller to get json data?
另外,我必须在我的页面中包含什么 javascript 以 (1) 在下拉列表中触发选择更改事件和 (2) 获取适当的数据(所选省份的城市列表)。
更新:
这是生成的页面源的sn-p:
<select Id="province_dll" class="form-control" data-val="true" data-val-required="The StartPointProvince field is required." id="StartPointProvince" name="StartPointProvince"><option value="NC">Northern Cape</option>
<option value="FS">Free State</option>
<option value="WC">Western Cape</option>
</select>
<select Id="city_dll" class="form-control" data-val="true" data-val-required="The StartPointCity field is required." id="StartPointCity" name="StartPointCity"><option value="NC">Kimberley</option>
<option value="FS">Boshof</option>
<option value="WC">Barkley</option>
</select>
这是我写的 javascript:
$("province_dll").change(function () {
$.ajax({
url: 'getCities/Trips',
type: 'post',
data: {
provinceId: $("province_dll").val()
}
}).done(function (response) {
$("cities_dll").html(response);
});
});
这是我的控制器(ps。数据只是测试数据,稍后将连接到db):
[HttpPost]
public ActionResult getCicites(int provinceId)
{
var lstCities = new SelectList(new[] { "City1", "City2", "City3" });
return Content(String.Join("", lstCities));
}
但它仍然无法正常工作 - 当我在第一个 DropDownList 中选择不同的值时,没有任何反应。
【问题讨论】:
标签: javascript c# ajax asp.net-mvc