2016-09-29 24 views
-2

我有一个选择列表,您可以在其中选择公交车站的位置。我希望地点成为选择列表项目,但我希望它返回与位置相关的公交车站点NUMBER。这怎么可能?我是否在新的SelectList()中声明它?鉴于asp.net MVC选择列表返回相关的值和选定的值

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); 
     } 

对应形式:

我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

是位置并具有一对一的连接停止编号(或从同一个表) ?如果是,则生成一个SELECT元素,其值为停止编号,文本为位置。 – Shyju

+0

'