视图中的表达式我在 mvc 名称 Flight 中有模型:
public class Flight
{
public int id { get; set; }
[Required(ErrorMessage = "name is required")]
[StringLength(170)]
public string name { get; set; }
[Required(ErrorMessage = "flight company is required")]
[DisplayName("company Name")]
public string flightCompany { get; set; }
[DataType(DataType.Date)]
public DateTime date { get; set; }
public int idAvaribleClass { get; set; }
[DisplayName("Duration TO")]
public string flightDuration { get; set; }
[DisplayName("Ariport Name")]
[StringLength(200)]
public string airportName { get; set; }
public int idRegisterFlght { get; set; }
public List<FlightDuration> FlightDurations { get; set; }
public List<AvalibleClass> AvalibleClasses { get; set; }
public List<registerFlightProgram> registerFlightPrograms { get; set; }
}
我有在数据库中搜索用户在视图中输入的内容并通过查询 linq 返回两个表的操作方法。我想在视图中显示此表:
_Mydb _db = 新 _Mydb();
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string searchTerm = null, string to = null)
{
var q =
(from c in _db.flights
join p in _db.Durations on c.id equals p.FlightId
where (p.to == to) && (p.fromStar == searchTerm) && (to != null)
select new
{
c = new Flight { id = c.id, name = c.name, flightCompany = c.flightCompany, date = c.date, idAvaribleClass = c.idAvaribleClass, flightDuration = c.flightDuration, airportName = c.airportName, idRegisterFlght = c.idRegisterFlght },
p = new FlightDuration { id = p.id, fromStar = p.fromStar, to = p.to, takeOffTime = p.takeOffTime, expectedTime = p.expectedTime, priceDuration = p.priceDuration, FlightId =p.FlightId}
}).SingleOrDefault();
return View(q);
}
*code view , expression in "@foreach"*
@model IEnumerable<TourismPro.Models.Flight>
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm()){
<form method="post">
<input type="search" name="searchTerm" />
<input type="search" name="to" />
<input type="submit" value="Search By Name" />
**@foreach (var item in Model)**
{
<div>
@item.flightCompany
</div>
}
</form>
}