私はSearchStudent()
アクションで作業しています。コードに問題があります。欠けているものがあります。 Partial View
を返し、私はSearch
フォームで最初Index page
内部Partial View Page
を表示したいと一度フォームがPost request
を使用して、それを提出いっぱい。私はそれがアヤックスから私たちのURLをハードコーディングする悪い習慣です知っているので、私は<p>
内の私のかみそりビューからdata-
属性を使用してい ..私はすべてのことがIndex view
内で起こるようにしたいので、すべてが使用してAJAXをしたいです要素は@Url.Action()
を使用して、URLそこに置きます。問題は、@Url.Action
を使用すると、SearchStudent() Action
の代わりにインデックスアクションにルーティングされ、partial view
の代わりに同じIndex page
が2回表示され、URLがajaxにハードコードされているときに、何が起こっているのかがわかりますか?razorとAjaxの@ Url.Action()を個別に使用して呼び出します。
たぶん、あなたは私のコードを見て、より良い理解します:
アクションメソッド(私は最初の部分図を表示したい)
public PartialViewResult SearchStudent()
{
return PartialView();
}
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult SearchStudent(string name)
{
List<Student> list = db.Students.Where(s => s.Name == name).ToList();
return Json(list, JsonRequestBehavior.AllowGet);
}
Indexビュー(部分図を示すように関わるだけのセクション) :
<div>
<p class="hand" id="pSearch" data-urlSearch="@Url.Action("SearchStudent","Students")"> Search by name</p>
<div id="ShowFormSearch"></div>
</div>
jQueryの機能(私はアヤックスurl: "Students/SearchStudent"
内に直接置く場合、それは動作しますが、それは良い習慣ではありません):
function ShowSearchPage()
{
$.ajax({
type: 'get',
url: $("#pSearch").data('urlSearch'),
}).success(function (result) {
$("#ShowFormSearch").html(result)
}).error(function() {
$("#ShowFormSearch").html("An error occurred")
})
}
それが動作するようになりました! html5 thxsでそれについて知らなかった – AlexGH