[英]Displaying data from a query using a drop down list selection mvc
我有點迷失在這個問題的編碼部分。 我有一個 MVC 應用程序,我試圖填充一個查詢Where子句作為下拉列表中的選定值。 此外,我正在從數據庫中的查詢填充下拉列表。 例如:
SELECT db.ID FROM Database db where ID = 1232
相反,我想做這樣的事情......
SELECT db.ID FROM Database db where ID = "SelectedValue from Dropdownlist"
型號分類:
public string ID {get; set;}
public string Summary{get; set;}
public int Estimate {get; set;}
public List<Hello> getIDs()
{
var que = (from wre in db.Table
select new Hello{
ID = wre.ID
}).toList();
}
控制器類:
public ActionResult Index(string? IDParam)
{
var model = test.getStuff();
var que = (from wre in db.View
where wre.ID == IDParam //Operator '==' cannot be applied to operands of type 'string' and 'string?'
select new Hello
{
ID = wre.ID
Summary = wre.Summary,
Estimate = wre.Estimate
}).toList();
if (IDParam!= null & IDParam.HasValue)
{
model = model.Where(x => x.ID == IDParam); //Operator '==' cannot be applied to operands of type 'string' and 'string?'
}
return View(model);
}
查看類:
@Html.DropDownList("ID", ViewBag.Releases as SelectList, "ID", new {@id="rel" })
<table>
<th>@Html.DisplayNameFor(model => model.ID)</th>
<th>@Html.DisplayNameFor(model => model.Summary)</th>
<th>@Html.DisplayNameFor(model => model.Estimate)</th>
</table>
<script>
$("#rel").change(function () {
var selectedId = this.val();
location.href = '@Url.Action("Index", "SampleController", new {selectedId="_selectedId"})'.Replace("_selectedId",selectedId);
});
</script>
這工作得很好,但現在,我迷失在它的編碼方面。 每次從下拉列表中更改 ID 時,我都可以看到警報。 但是,顯示的數據沒有變化(我知道我遺漏了很多)。 如果有人可以在這里幫助我,那就太好了,謝謝!
1) 嘗試在 Linq 中添加 where 子句
where wre.ID == IDParam
2) 將 que 更改為您的類的實例並獲得 firstOrDefault 而不是 List
3)嘗試將您的類的實例作為模型傳遞給視圖
return View(test);
1- 您需要更改 Id 如下:
$("#rel").change(function () {
// alert("Changed");
var selectedId =this.value;
});
2- 如果你想重新加載你的頁面,那么你需要為你的[HttpGet]
方法設置一個parametre
:
public ActionResult Index(int? selectedId) // selectedId
{
var que = (from wre in db.View
select new Hello
{
ID = IDParam
Summary = wre.Summary,
Estimate = wre.Estimate
}).toList();
ViewBag.Releases = new SelectList(test.getIDs(), "", "ID");
var model = test.getStuff();
if(selectedId!=null && selectedId.HasValue)
{
// do some stuff with your selectedId
// if working method is test.getStuff(), then maybe you can use like code below:
model = model.Where(x=> x.Id==selectedId);
}
return View(model);
}
並在您的視圖中:
$("#rel").change(function () {
// alert("Changed");
var selectedId =this.value;
location.href = '@Url.Action("Index", "YourController", new {selectedId="_selectedId"})'.Replace("_selectedId",selectedId);
});
3- 如果你不想重新加載頁面,那么你需要使用Ajax
請求,你必須創建一個新的方法來通過 selectedId 獲取數據,
// 你的觀點。
$("#rel").change(function () {
// alert("Changed");
var selectedId =this.value;
$.ajax({
method: "POST",
url: "'@Url.Action("GetDataBySelectedId","YourController")'",
data: { selectedId: selectedId }
})
.done(function( data) {
// Delete your table, and fill table with your data with jquery.
});
});
// 你的控制器:
[HttpPost]
public JsonResult GetDataBySelectedId(int? selectedId)
{
// Do some stuff for get your data.
var data = yourData.Where(x=> x.Id = selectedId.Value);
return Json(data);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.