选择trainnumber之后如何通过这个选择的trans的控制器countvagon并进入视图下拉列表 [英] How after choosing trainsnumber pass to controller countvagon of this choosed trans and get in view dropdownlist
问题描述
嗨。我是新来的。我知道小角度,asp.net mvc,jquery,json但不能使用它们并在一个项目中合并。所以我有下拉列表 - contry,trans名称,vagon Name.I想要选择trainName,之后在数据库中找到这个选择的Train的Vagon。并显示这个vagon的下拉列表。
据我所知,我需要从View传递给Controller哪个列车选择,然后从数据库查找CountVagon(例如Train-T0000000023有5个vagon)并从Controller传递到View DropdownList(包括-Vagon1,Vagon2,Vagon3,Vagon4,Vagon5)。
请解释一下,如果有人可以解决这个问题。我已经搜索了很多次但是不能做得很好我想要的东西。谢谢你:))
我尝试过:
Hi.I am new here.I know little angular,asp.net mvc ,jquery,json but cannot use them and combine in one project.So I have dropdown list-contry,trans Name,vagon Name.I want choose trainsName,after that find in database count Vagon of this choosen Train.And show dropdown list this vagon.
As I understand I need to pass from View to Controller which train is choosen,then find from database CountVagon(for example Train-T0000000023 has 5 vagon) And pass from Controller to View DropdownList(which consist-Vagon1,Vagon2,Vagon3,Vagon4,Vagon5).
Please explain me,if somebody can solve that.I have searched many time but couldnt do exatly what I wanted.Thank you :))
What I have tried:
<script type="text/javascript">
$(document).ready(function () {
$('#Name').change(function () {
var TrainID= $('#Name :selected').val();
});
});
</script>
<h2>Buy</h2>
<div class="table table-bordered">
<div class="filter">
@Html.DropDownListFor(x => x.City, (SelectList)ViewBag.TrainsCity)
@Html.DropDownListFor(x => x.Name, (SelectList)ViewBag.TrainsName)
@Html.DropDownListFor(x => x.VagonCount, (List<SelectListItem>)ViewBag.TrainsVagon)
</div>
ANd Controller
public ActionResult Buy()
{
ViewBag.TrainsName = new SelectList(db.Trains, "ID", "Name");
ViewBag.TrainsCity = new SelectList(db.Trains, "ID", "City");
//which train or where choosed angular will receive and get the number of train by using lambda expression and pass to the methods
//I need to pass countVagon instead of 7
ViewBag.TrainsVagon = PopulateCountVaqon(7);
PopulateCountVaqon(7);
IEnumerable<TrainReqistration.Models.Train> trains = db.Trains;
return View();
}
List<SelectListItem> PopulateCountVaqon(int count)
{
List<SelectListItem> countVagon = new List<SelectListItem>();
for(int i = 0; i <= count; i++)
{
countVagon.Add(new SelectListItem() { Value = i.ToString(), Text = "Vaqon " + i });
}
return countVagon;
}
推荐答案
(document).ready(function(){
(document).ready(function () {
('#Name')。change(function(){
var TrainID =
('#Name').change(function () { var TrainID=
('#Name:selected')。val();
});
});
< / script>
< h2>购买< / h2>
< div class =table table-bordered>
< div class =filter>
@ Html.DropDownListFor(x => x.City,(SelectList)ViewBag.TrainsCity)
@ Html.DropDownListFor(x => x.Name,( SelectList)ViewBag.TrainsName)
@ Html.DropDownListFor(x => x.VagonCount,(List< SelectListItem>)ViewBag.TrainsVagon)
< / div>
和控制器
公共ActionResult买()
{
ViewBag.TrainsName = new SelectList (db.Trains,ID,Name);
ViewBag.TrainsCity = new SelectList(db.Trains,ID,City);
//哪个火车或选择角度的地方将通过使用lambda表达式接收并获得火车数量并传递给方法
//我需要通过countVagon而不是7
ViewBag.TrainsVagon = PopulateCountVaqon(7);
PopulateCountVaqon(7);
IEnumerable< TrainReqistration.Models.Train> train = db.Trains;
return View();
}
列表< SelectListItem> PopulateCountVaqon(int count)
{
List< SelectListItem> countVagon = new List< SelectListItem>();
for(int i = 0; i< = count; i ++)
{
countVagon.Add(new SelectListItem(){Value = i.ToString(),Text =Vaqon + i});
}
返回countVagon;
}
('#Name :selected').val(); }); }); </script> <h2>Buy</h2> <div class="table table-bordered"> <div class="filter"> @Html.DropDownListFor(x => x.City, (SelectList)ViewBag.TrainsCity) @Html.DropDownListFor(x => x.Name, (SelectList)ViewBag.TrainsName) @Html.DropDownListFor(x => x.VagonCount, (List<SelectListItem>)ViewBag.TrainsVagon) </div> ANd Controller public ActionResult Buy() { ViewBag.TrainsName = new SelectList(db.Trains, "ID", "Name"); ViewBag.TrainsCity = new SelectList(db.Trains, "ID", "City"); //which train or where choosed angular will receive and get the number of train by using lambda expression and pass to the methods //I need to pass countVagon instead of 7 ViewBag.TrainsVagon = PopulateCountVaqon(7); PopulateCountVaqon(7); IEnumerable<TrainReqistration.Models.Train> trains = db.Trains; return View(); } List<SelectListItem> PopulateCountVaqon(int count) { List<SelectListItem> countVagon = new List<SelectListItem>(); for(int i = 0; i <= count; i++) { countVagon.Add(new SelectListItem() { Value = i.ToString(), Text = "Vaqon " + i }); } return countVagon; }
这篇关于选择trainnumber之后如何通过这个选择的trans的控制器countvagon并进入视图下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!