【发布时间】:2014-10-25 19:10:54
【问题描述】:
我在控制器中有一个 ActionResult 用于反馈页面,我正在尝试使用级联下拉列表,通过将度假村列表限制为先前选择的国家/地区来简化 UI 的使用。
当使用 ajax(json) 回调控制器时,页面索引的 ActionResult 被调用,因此级联列表不起作用。
[HttpPost]
public ActionResult GetResorts(string selectedcountry)
{
using (HalsburyEntities ctx = new HalsburyEntities())
{
List<tblSkiResort> resorts = (from r in ctx.tblSkiResorts
join c in ctx.tblGlobalCountries on r.countryId equals c.CountryID
where c.CountryName == selectedcountry
select r).ToList();
return Json(resorts, JsonRequestBehavior.DenyGet);
}
}
[HttpPost]
public ActionResult Index(SkiTripEnquiryViewModel stevm)
{
// Do Stuff
return RedirectToAction("thanks");
}
public ActionResult Thanks()
{
return View();
}
public ActionResult Index(string resort, string hotelname)
{
//DoStuff
return View(stevm);
}
由于 Index 的 ActionResult 被调用,致谢页面总是返回到 AJAX Post 中“成功”的数据中。
JS
$(function () {
$('#Country').change(function(){
$.ajax({
url: '@Url.Action("GetResorts", "SkiTripEnquiry")',
type: "POST",
datatype: "JSON",
data: { 'selectedcountry': $(this).val() },
success: function (data) {
alert("Hello World");
}
});
});
});
CSHTML - 表单开始到下拉字段集结束
@using (Html.BeginForm("SubmitEnquiry", "SkiTripEnquiry", FormMethod.Post))
{
@Html.ValidationSummary(true, "Please correct the errors")
<span style="color:#ff0000;font-weight:bold">@Model.SuccessMessage</span>
<fieldset>
<legend>Resort:</legend>
<div class="enquiryDestination ">
<table style="clear: both;">
<tr id="" style="height: 50px; padding: 5px;">
<td style="width: 187px;">
Country
</td>
<td>
@Html.DropDownListFor(x => x.Country, Model.Countries, "All", new { style = "width:212px; padding: 5px;", onfocus = "showHelp('box2', 'Countries');", name = "Countries;" })
</td>
</tr>
<tr id="" style="height: 50px; padding: 5px;">
<td style="width: 187px;">
Destination
</td>
<td>
@*@Html.DropDownListFor(x => x.ResortName, Model.ResortNames, "All", new { style = "width:212px; padding: 5px;", onfocus = "showHelp('box2', 'ResortNames');", name = "ResortNames;" })*@
Resort:
@Html.DropDownListFor(x => x.SelectedResort, Enumerable.Empty<SelectListItem>())
</td>
</tr>
<tr id="" style="height: 50px; padding: 5px;">
<td style="width: 187px;">
</td>
<td>Add Destination</td>
</tr>
</table>
</div>
</fieldset>
我不确定我错过了什么。特别是当我按名称引用确切的操作结果时。我也尝试将 GetResorts 方法变成 JsonResult
编辑:它看起来毕竟是路由。当前路由是:
routes.Add(new Route("skitripenquiry/thanks",
new RouteValueDictionary(
new { controller = "skitripenquiry", action = "Thanks" }),
new HyphenatedRouteHandler()));
routes.Add(new Route("skitripenquiry/{resort}/{hotelname}",
new RouteValueDictionary(
new { controller = "skitripenquiry", action = "Index", resort = "", hotelname = "" }),
new HyphenatedRouteHandler()));
因此我添加了:
routes.Add(new Route("skitripenquiry/GetResort",
new RouteValueDictionary(
new { controller = "skitripenquiry", action = "GetResort", selectedcountry = "" }),
new HyphenatedRouteHandler()));`
但这不起作用
【问题讨论】:
-
您的代码看起来不错,问题一定出在其他地方。
-
任何关于可能在哪里的线索?
-
也许您可以尝试使用 ajax 方法:$(this).find(":selected").val();而不仅仅是 $(this).val()。在我的一个项目中,我遇到了这个问题,并且一切正常,也许你可以试一试
-
@JorgeF 恐怕不走运,不过是个好主意!,我也尝试将其更改为 $('#Country').val() 无济于事
-
所以调用 Index 操作而不是 GetResorts 操作?你有包装控件的表单吗?如果是的话,你能把那个也发一下吗?
标签: jquery ajax asp.net-mvc json asp.net-mvc-3