ドロップダウンリストから選択した値をどこにフィルタリングするのがベストプラクティスですか。したがって、ここに行く:DropDownListの選択時に、Ajaxパーシャルビュー、MVCを使用したSQLクエリのフィルタリング
私は選択された値は、SQLクエリをフィルタリングし、ドロップダウンリストのIDのIDに関連するビデオを表示したいと思います。現時点では、データベース内の最初のビデオのみを10回表示しています。データベースには10個のビデオがあり、ID = IDで表示されているので、わかりました。ドロップダウンリストで選択したアイテムに関連するものが部分ビュー内に表示されます。
ビューモデル
using System.Web.Mvc;
using System.Collections.Generic;
namespace ACGteam.ViewModels
{
public class AthleteVideoViewModel
{
public List<AthleteVideoViewModel> AthleteDbList { get; set; }
public int anAthleteID { get; set; }
public int AthleteID { get; set; }
public string AthleteName { get; set; }
public int AthleteVideoID { get; set; }
public string AthleteVideo { get; set; }
public IEnumerable<SelectListItem> AthleteListItems
{
get { return new SelectList(AthleteDbList, "AthleteID", "AthleteName"); }
}
public IEnumerable<SelectListItem> AthleteVideosList
{
get { return new SelectList(AthleteDbList, "anAthleteID", "AthleteVideo"); }
}
}
}
CONTROLLER
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.Generic;
using ACGteam.Models;
using ACGteam.ViewModels;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using ACGteam.Services;
using System.Web.UI.WebControls;
using System.Data.Entity.Infrastructure;
namespace ACGteam.Controllers
{
public class VideosController : Controller
{
public ActionResult VideoPage()
{
return View();
}
public ActionResult VideoPage2016()
{
var ddlAthleteSelected = new AthleteVideoViewModel();
ddlAthleteSelected.AthleteDbList = PopulateAthleteNames();
return View(ddlAthleteSelected);
}
private List<AthleteVideoViewModel> PopulateAthleteNames()
{
using (SqlConnection connection = new SqlConnection(DatabaseConnection.GetConnectionString("acgvideodbConnectionString")))
{
using (SqlCommand cmd = connection.CreateCommand())
{
try
{
cmd.CommandText = "SELECT AthleteID, AthleteName AS Athlete FROM Athlete_Name ORDER BY Athlete";
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
sda.Fill(ds);
var items = ds.Tables[0].AsEnumerable().Select(dataRow => new AthleteVideoViewModel { AthleteID = dataRow.Field<int>("AthleteID"), AthleteName = dataRow.Field<string>("Athlete") }).ToList();
return items;
}
}
finally
{
}
}
}
}
public PartialViewResult GetAthleteVideo(int AthleteID)
{
var vidModels = new AthleteVideoViewModel();
vidModels.AthleteDbList = PopulateVideosWithAthleteID();
var vid = vidModels.AthleteDbList.Where(a => a.anAthleteID == AthleteID).FirstOrDefault();
vidModels.AthleteVideo = vid.AthleteVideo;
return PartialView("_ShowAthlete", vidModels);
}
private List<AthleteVideoViewModel> PopulateVideosWithAthleteID()
{
using (SqlConnection connection = new SqlConnection(DatabaseConnection.GetConnectionString("acgvideodbConnectionString")))
{
using (SqlCommand cmd = connection.CreateCommand())
{
try
{
List<AthleteVideoViewModel> vidmod = new List<AthleteVideoViewModel>();
cmd.CommandText = "SELECT Athlete_Name.AthleteID AS AthleteID, Athlete_Video.anAthleteID AS anAthleteID, Athlete_Video.AthleteVideo AS AthleteVideo FROM Athlete_Video INNER JOIN Athlete_Name ON Athlete_Name.AthleteID = Athlete_Video.anAthleteID";
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
DataTable dt = new DataTable();
ds.Tables.Add(dt);
vidmod = ds.Tables[0].AsEnumerable().Select(dataRow => new AthleteVideoViewModel { AthleteID = dataRow.Field<int>("AthleteID"), anAthleteID = dataRow.Field<int>("anAthleteID"), AthleteVideo = dataRow.Field<string>("AthleteVideo") }).ToList();
return vidmod;
}
finally
{
}
}
}
}
}
}
メインビュー
@model ACGteam.ViewModels.AthleteVideoViewModel
@{
ViewBag.Title = "VideosPage2016";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container">
<div class="box">
<h2 style="text-align:center">ACG 2015-2016 Competition Season</h2>
<h2 style="text-align:center"> Athlete Videos </h2>
<div class="row">
<div class="col-lg-12" style="margin:auto 0; display:block">
@Html.DropDownListFor(m => m.AthleteID, Model.AthleteListItems, new { Class = "imgcenter", id = "ddlAthlete" })
</div>
</div>
<div id="partialDiv">
@Html.Partial("_ShowAthlete")
</div>
<script>
$(document).ready(function() {
$("#ddlAthlete").on("change", function() {
$.ajax(
{
url: "GetAthleteVideo?AthleteID=" + $(this).prop("value"),
type: "GET",
data: "",
contentType: "application/json; charset=utf-8",
success: function (data)
{
$("#partialDiv").html(data);
},
error: function() { alert("error"); }
});
});
});
</script>
部分図
@model ACGteam.ViewModels.AthleteVideoViewModel
@{
ViewBag.Title = "_ShowAthlete";
}
<div class="row">
<div style="display: block; text-align: center;">
<div>
<img height="75" width="100" src="/Img/AvantCoeurLogoGirls.png" />
</div>
<div class="col-md-12 display-label">
@Html.DisplayFor(items => items.AthleteName)
</div>
<div class="box">
<div class="row">
@foreach (var athVid in Model.AthleteDbList)
{
<div class="col-md-6 col-lg-4 img-responsive">
<video style="height:250px; width:250px" controls="controls">
<source src="@Html.DisplayFor(vidmod => vidmod.AthleteVideo)" type="video/mp4" />
</video>
<br />
<strong>GroupShow_Candyland</strong>
</div>
}
This is the Query Result from the Method, PopulateVideosWithAthleteID
問題は部分的な見解です。しかし、前提に基づいてソリューションを提供する前に、次の質問に答えることができますか? 1人の選手には1つのビデオしかないのですか?メソッド 'PopulateVideosWithAthleteID'は' athleteId'に基づいてそれらをフィルタリングせずにすべてのビデオをdbから読み込みます意図的ですか?あなたのデータベースからいくつかのサンプルデータを共有できれば助かります。 –
返信いただきありがとうございます! 1人のアスリートにとって1つのビデオしか存在しない可能性があります。それは意図的ではない、私はこれと思った。どこ(a => a.anAthleteID == AthleteID)は私のためのフィルタリングを行うだろう。私も、動作するPopulateVideosWithAthleteIDメソッドでathleteIDをフィルタリングする必要があります。あなたがこれで流すことができる光は役に立ちます! – runningtree