我一直在試圖獲得在asp.net mvc4中工作的級聯下拉菜單....現在我開始使用ajax.beginform ...後來我可能會去直接jquery .. ..mvc ajax.beginform不能與partialview
我的問題是,調用第一個(軌道)ajax.beginform,它調用控制器,然後這指向第二個部分(鐵路)...但是,當SelectRailPartial.cshtml服務器它這樣做全視圖...它不是與div標籤作爲替代品...我已經嘗試了各種值在ajaxControls中指定替換,等等...他們似乎沒有什麼不同...
我懷疑我的問題可能在JavaScript我包括....我有條紋編輯一切回到一些我知道我需要保持簡單....任何人都可以發現我做錯了什麼?請....
在我的(移動)控制器我有以下兩種方法:
//this is my main form that I am looking at...
public ActionResult LoadRailVehicle()
{
LoadRailVehicleInputModel model = new LoadRailVehicleInputModel();
IRailService railService = new RailService();
model.TrackList = railService.GetActiveRailTracks().ToList();
return View(model);
}
[HttpPost]
public ActionResult SelectTrack(string selectedTrack)
{
LoadRailVehicleInputModel model = new LoadRailVehicleInputModel();
//kludge for now...
model.RailCarList.Add("aaaa");
return PartialView("xSelectRailPartial", model);
}
我LoadRailVehicle.cshtml
@using TBS.Etracs.Web.Main.Areas.Carrier.Models
@model LoadRailVehicleInputModel
@{
ViewBag.Title = "Load Rail Vehicle";
}
<scripts>
<script src="@Url.Content("~/Scripts/jquery-1.9.1.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.9.1.intellisense.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.10.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
</scripts>
<table border="0">
<tr>
<td>Track</td>
<td> : </td>
<td><div id="trackdiv">@Html.Partial("xSelectTrackPartial", Model)</div></td>
</tr>
<tr>
<td>Rail</td>
<td> : </td>
<td><div id="Raildiv">@Html.Partial("xSelectRailPartial", Model)</div></td>
</tr>
</table>
在xSelectTrackPartial.cshtml:
@using TBS.Etracs.Web.Main.Areas.Carrier.Models
@model LoadRailVehicleInputModel
<scripts>
<script src="@Url.Content("~/Scripts/jquery-1.10.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
</scripts>
@using (Ajax.BeginForm("SelectTrack", "Mobile",
new AjaxOptions
{ UpdateTargetId = "Raildiv"
@* This is a control in theLoadRailVehicle.cshtml *@
}))
{
@Html.DropDownListFor(p => p.SelectedTrack,
new SelectList(Model.TrackList),
new { id = "trackdropdown",
name = "trackdropdown",
Class = "dropdownstyle",
style = "width: 100px" ,
onchange = "this.form.submit();"})
}
在xSelectRailPartial.cshtml
@using TBS.Etracs.Web.Main.Areas.Carrier.Models
@model LoadRailVehicleInputModel
<scripts>
<script src="@Url.Content("~/Scripts/jquery-1.10.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
</scripts>
@if (Model.RailCarList != null && Model.RailCarList.Count() > 0)
{
@Html.DropDownListFor(p => p.SelectedRailCar,
new SelectList(Model.RailCarList),
new { id = "raildropdown",
name = "raildropdown",
Class = "dropdownstyle",
style = "width: 100px" })
}
這裏是我_layout.cshtml這我通過_viewstart
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title - Etracs</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
@* @Styles.Render("~/Content/css") *@
@* @Scripts.Render("~/bundles/modernizr") *@
@* @Scripts.Render("~/bundles/jquery") *@
<script src="@Url.Content("~/Scripts/jquery-1.9.1.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.9.1.intellisense.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.10.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@RenderSection("scripts", required: false)
</head>
<body>
<header>
<div class="content-wrapper">
<div class="float-right">
<section id="login">
@Html.Partial("_LoginPartial")
</section>
</div>
</div>
</header>
<div id="body">
<section class="content-wrapper main-content clear-fix">
@RenderBody()
</section>
</div>
@Html.Partial("_ViewSwitcher")
<footer>
<div class="content-wrapper">
<div class="float-left">
<p>© @DateTime.Now.Year - Turning Basin Services</p>
</div>
</div>
</footer>
</body>
</html>
我決定切換到直Jquery自從我讀了,你不能有一個html.beginform(我最終需要)內的ajax.beginform .... – user2040849 2013-02-25 20:17:49
經過很多小問題,我得到了jquery工作.....對於那些來在我之後,閱讀了這個沒有想到的問題之一就是需要JsonResult調用的JsonRequestBehavior ... – user2040849 2013-02-25 20:20:05
即。 public JsonResult GetRailCars(string selectedTrack) { IRailCarService railCarService = new RailCarService(); LoadRailVehicleInputModel model = new LoadRailVehicleInputModel(); 列表 railCarList =新列表(); if(selectedTrack!= null && selectedTrack!= string.Empty) { // railCarList.Add(「aaaa」); railCarList.Add(「bbbb」); } return Json(railCarList,JsonRequestBehavior.AllowGet); } –
user2040849
2013-02-25 20:20:48