請教大家,我想在一個View顯示二個資料表,一個是行程表的全部,
另一個航班表是取行程表的fFgroup航班編號來做關聯==航班表的fId,行程表是全部會列出,
可是點選航班的按鈕,要透過Home/Product?id=的值來印出,有什麼辦法可以改進。
大概的樣子就像是表格檢視全部商品,然後圖片裡的航班按鈕就像是編輯取ID=xx
檢視到另一張表格的內容,
我是使用Bootstrap的Modal點進去是跳一個小視窗我給她裡面塞表格這樣,https://getbootstrap.com/docs/4.0/components/modal/ ,我想問的是因為如果用超連結 Home/Product?id=@i.id 這樣的值是對的,可是因為用超連結加上是在同一頁會get到新頁面然後再點就是死循環了,想問有沒有什麼方法可以修改過來這個問題。
//產品 控制器
public ActionResult Product(int?fid)
{
HomeProAviViewModel ViewModel = new HomeProAviViewModel();
{
ViewModel.Project = db.tProjects.ToList();
ViewModel.Aviation = db.tAviations.Where(m => m.fId ==fid).ToList();
return View(ViewModel);
}
}
public class HomeProAviViewModel
{
public List<tProject> Project{get;set; }
public List<tAviation> Aviation { get; set; }
}
//航班表
public partial class tAviation
{
public int fId { get; set; }
[DisplayName("天數")]
public int fDay { get; set; }
[DisplayName("航空公司")]
public string fAirline { get; set; }
[DisplayName("A起飛時間")]
public string fAonetime { get; set; }
[DisplayName("A降落時間")]
public string fAendtime { get; set; }
[DisplayName("出發地")]
public string fDeparure { get; set; }
[DisplayName("目的地")]
public string fDestination { get; set; }
[DisplayName("B起飛時間")]
public string fBonetime { get; set; }
[DisplayName("B降落時間")]
public string fBendtime { get; set; }
}
//行程表
public partial class tProject
{
public partial class tProject
{
public int fId { get; set; }
[DisplayName("國家")]
public string fCountry { get; set; }
[DisplayName("區域")]
public string fStroke { get; set; }
[DisplayName("天數")]
public int fDay { get; set; }
[DisplayName("機位")]
public int fPosition { get; set; }
[DisplayName("出發時間")]
public System.DateTime fTime { get; set; }
[DisplayName("圖片")]
public string fImg { get; set; }
[DisplayName("價錢")]
public int fPrice { get; set; }
[DisplayName("航班編號")]
public int fFgroup { get; set; }
[DisplayName("產品名稱")]
public string fTrip { get; set; }
[DisplayName("產品編號")]
public int fTripId { get; set; }
public string lnner_one { get; set; }
public string lnner_two { get; set; }
public string lnner_three { get; set; }
public string lnner_four { get; set; }
}
}
<div class="col-lg-9 mt-3">
<div class="row">
@foreach (var i in Model.Project)
{
<div class="col-lg-4 col-md-6 mt-3">
<div class="card ml-2 h-100">
<img src="~/img/top/@i.fImg" class="card-img-top">
<div class="line1 mt-2">
<div class="d-inline mb-2">旅遊天數: @i.fDay</div>
<div class="d-inline float-right mb-2">出發日期:@i.fTime</div></div>
<div class="card-body"><h6 class="card-text">@i.fTrip</h6></div>
<div class="myline">
<div class="d-inline" id="mytable"><input type="hidden" value="@i.fFgroup" /><span class="btn btn-primary w-40" data-toggle="modal"
data-target="#exampleModalCenter">航班</span> </div>
<div class="d-inline float-right mt-2"><h6>機位: @i.fPosition</h6>/div</div>
<div></div>
<div class="myline">
<div class="d-inline "><a href="#" class="btn btn-primary w-40">報名</a>/div>
<div class="d-inline float-right text-danger mt-2 h5"><span>$</span>@i.fPrice <span>起</span></div>
</div>
</div>
</div>
}
</div>
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">航班資訊</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button></div>
<div class="modal-body">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">天數</th>
<th scope="col">航空公司</th>
<th scope="col">起飛時間</th>
<th scope="col">抵達時間</th>
<th scope="col">出發地</th>
<th scope="col">目的地</th>
</tr>
</thead>
<tbody>
@foreach (var i in Model.Aviation)
{
<tr>
<td>@i.fId</td>
<td>@i.fAirline</td>
<td>@i.fAonetime</td>
<td>@i.fAendtime</td>
<td>@i.fDeparure</td>
<td>@i.fDestination</td>
</tr>
<tr>
<td>@i.fDay</td>
<td>@i.fAirline</td>
<td>@i.fBonetime</td>
<td>@i.fBendtime</td>
<td>@i.fDestination</td>
<td>@i.fDeparure</td>
</tr>
}
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
您可以先在Controller寫一個JsonResult
public JsonResult GetProduct(int? fid)
{
// 取得Product資訊
return Json(Product, JsonRequestBehavior.AllowGet); // 回傳Json資訊
}
然後在前端用Ajax取得資料,然後將資訊組回去
$.ajax({
url: // GetProduct's URL
data: { fid: yourFid },
success: function(data, textStatus, jqXHR) {
// 將data組回去
},
error: function(jqXHR, textStatus, errorThrown ) {
// error control
}
})