asp.net MVC选择列表返回相关值和选中的值

时间:2016-09-29 18:34:37

标签: c# asp.net asp.net-mvc controller selectlist

我有一个选择列表,您可以在其中选择公交车站的位置。我希望位置是选择列表项,但我希望它返回与该位置相关的公交车站号NUMBER。这怎么可能?我是否在新的SelectList()中声明了它?

我的routeStop Controller创建功能:

   public IActionResult Create()
        {
            ViewData["BusRouteCode"] = new SelectList(_context.BusRoute, "BusRouteCode", "BusRouteCode");
            ViewData["Location"] = new SelectList(_context.BusStop, "Location", "Location");



            return View();
        }

        // POST: DwRouteStop/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("RouteStopId,BusRouteCode,BusStopNumber,OffsetMinutes")] RouteStop routeStop)
        {

            if (ModelState.IsValid)
            {


                _context.Add(routeStop);
                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewData["BusRouteCode"] = new SelectList(_context.BusRoute, "BusRouteCode", "BusRouteCode", routeStop.BusRouteCode);
            ViewData["Location"] = new SelectList(_context.BusStop, "Location", "Location",  routeStop.BusStopNumberNavigation.Location);
            return View(routeStop);
        }

相应的表格:

 <div class="form-group">
            <label asp-for="BusStopNumberNavigation.Location" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <select asp-for="BusStopNumberNavigation.Location" class ="form-control" asp-items="ViewBag.Location"></select>
            </div>
        </div>

RouteStop模型:

namespace DwBusService.Models
{
    public partial class RouteStop
    {
        public int RouteStopId { get; set; }
        public string BusRouteCode { get; set; }
        public int? BusStopNumber { get; set; }





        public int? OffsetMinutes { get; set; }

        public virtual BusRoute BusRouteCodeNavigation { get; set; }
        public virtual BusStop BusStopNumberNavigation { get; set; }



    }
}

巴士站模型:

namespace DwBusService.Models
{
    public partial class BusStop
    {
        public BusStop()
        {
            RouteStop = new HashSet<RouteStop>();
            TripStop = new HashSet<TripStop>();
        }

        public int BusStopNumber { get; set; }
        public string Location { get; set; }
        public int LocationHash { get; set; }
        public bool GoingDowntown { get; set; }

        public virtual ICollection<RouteStop> RouteStop { get; set; }
        public virtual ICollection<TripStop> TripStop { get; set; }
    }
}

我可以通过busStopNumberNavigation访问位置和公交车站号码。我希望你能理解我的问题。我想选择位置并同时将位置和公交车站号码返回到后期操作。

1 个答案:

答案 0 :(得分:0)

_context.BusStop创建一个列表,如:

_context.BusStopLIst=BusStops.Select(BusStop=> new { Text = BusStop.Location , Value = BusStop.BusStopNumber  });

然后创建您的ViewData

ViewData["BusStops"]=new SelectList(_context.BusStopLIst, "Value", "Text");