私はコントローラのアクションを以下のビューきのasp.net MVCで_layoutページからAjaxの検索ポップアップ
public ActionResult Dashboard()
{
RepositoryClass sample= new RepositoryClass();
ViewBag.listDetails = sample.GetDetails(null, null);
return View();
}
ダッシュボードビュー
@{
ViewBag.Title = "Dashboard";
Layout = "~/Views/Shared/_ayout.cshtml";
}
<table>
<thead>
<tr>
<th>ID</th>
</tr>
</thead>
<tbody>
@foreach (var item in ViewBag.listDetails)
{
<tr>
<td>@item.ID</td>
</tr>
}
</table>
だから私は、このために、ポップアップ検索を追加して検索しますページを更新せず、結果、さらに
私は_layout PAGにポップアップ検索フォームを追加しました
<!DOCTYPE html>
<html>
<head>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<!-- Begin page -->
<div id="wrapper">
<div class="content-page">
<div class="content">
<div class="container">
@RenderBody()
</div>
</div>
</div>
</div>
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
<form role="form" class="sss">
<h3 class="panel-title text-dark text-center">Select Date Range</h3>
<input type="text" id="startdate" name="startdate" class="inn">
<input type="text" id="enddate" name="enddate" class="inn">
<button id="btnSearch" type="button" class="ss">Search</button>
</form>
</body>
</html>
<script type="text/javascript">
$(document).ready(function() {
$('#btnSearch').click(function() {
alert("button clicked");
$.ajax({
type: "POST",
url: "/Home/SearchbyDates",
contentType: "application/json; charset=utf-8",
data: { startdate : document.getElementById('startdate').value, enddate: document.getElementById('enddate').value, },
dataType: "json",
Success: function (response)
{
alert("Success");
$('table tbody').html(response);
},
error: function()
{
alert("error");
}
});
return false
});
});
</script>
を次のようにeはその後、私は、検索結果を取得し、ページに
public ActionResult Dashboard()
{
RepositoryClass sample= new RepositoryClass();
ViewBag.listDetails = sample.GetDetails(null, null);
return View();
}
[HttpPost]
public ActionResult SearchbyDates(DateTime? startdate , DateTime? enddate)
{
RepositoryClass sample= new RepositoryClass();
ViewBag.listDetails = sample.GetDetails(startdate, enddate);
return Json(ViewBag.listDetails , JsonRequestBehavior.AllowGet);
}
を更新せずに同じDashboard
ビューに表示しかし、私はbtnSearch
ボタンをクリックしたときに、私は私の警告を見ることができますし、次のコントローラのアクションを追加しましたここに置くが、私はこれをデバッグするときにSearchbyDates
メソッドに指示しない。
エラー警告のみが表示されます。私のアプローチで何が間違っている
削除 'contentTypeの両方ご
Dashboard()
とSearchbyDates()
方法でViewBag
を使用して、むしろという、モデルを渡すことをお勧めします:「アプリケーション/ JSON。あなたのデータをひどく傷つけることはありません –@StephenMueckeはメソッドへの指示を行いますが、開始日の終了日の値はnullです – kez
@StephenMueckeここで私のデータを酷評することができますか? – kez