使用下拉列表选择mvc显示查询中的数据

时间:2018-10-29 21:07:22

标签: javascript jquery asp.net-mvc entity-framework

在这个问题的编码部分,我有点迷失了。我有一个MVC应用程序,并且尝试填充查询其中子句作为下拉列表中的选定值。另外,我正在从数据库中的查询填充下拉列表。例如:

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时,我都可以看到警报。但是,显示的数据没有变化(我知道我很想念)。如果有人可以在这里帮助我,那就太好了,谢谢!

2 个答案:

答案 0 :(得分:0)

1)尝试在Linq中添加where子句

where wre.ID == IDParam

2)将que更改为类的实例,并获取firstOrDefault而不是List

3)尝试将类的实例作为模型传递给视图

return View(test);

答案 1 :(得分:0)

1-您需要按以下方式更改ID:

$("#rel").change(function () {
              // alert("Changed");
var selectedId =this.value;
        });

2-如果要重新加载页面,则需要将parametre设置为[HttpGet]方法:

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