3

小さな問題があります。

私の行動は:

 public ViewResult SearchForRooms(int HotelDDL) 
    {

        List<Room> roomsInHotel = bl.ReturnRoomsPerHotel(HotelDDL);

        return View(roomsInHotel);
    }

アクションを呼び出しているjqueryは次のとおりです。

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $("#HotelDDL").change(function () {

            var text = $("#HotelDDL option:selected").text();
            var value = $("#HotelDDL option:selected").val();
            alert("Selected text=" + text + " Selected value= " + value);

            $.post("/Home/SearchForRooms",
            {
                HotelDDL: $("#HotelDDL option:selected").val() 
            });
        });
    });
</script>

そして最後に、これが呼び出されるべきビューです:

@model IEnumerable<RoomReservation.Entities.Entities.Room>

@{
    ViewBag.Title = "Search";
}

<h2>Result</h2>

<table>
    <tr>
        <th>
            City
        </th>
        <th>
            Hotel
        </th>
        <th>
            Room label
        </th>
        <th>
            Number of beds
        </th>
        <th>
            Price per night
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelitem=>item.Hotel.City.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Hotel.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NumberOfBeds)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PricePerNight)
        </td>
    </tr>
}
</table>

最終的なビューのレンダリングを除いて、すべてが正常に機能しています(databseはすべての部屋を正しく返します)。Philのツールを試しましたが、疑わしいヒントは得られません。

RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);

では、jscriptがpostメソッドを送信した後に表示されないのはなぜSearchForRooms()ですか?ありがとうございました

PS他のコードが必要な場合は、そのように言ってください。

4

1 に答える 1

3

ajax投稿を実行しても、従来の投稿のようにページが更新またはリダイレクトされないことに注意してください。

ビューを配置する場所がないため、ビューは表示されていません。正常に投稿し、ビューを正常に返しますが、その後は何もしません。返されたビューを表示したい場合は、なんらかの方法でそれをdomに追加する必要があります。一般的な方法は次のとおりです。

<div id="successDiv"></div>
<script type="text/javascript" language="javascript">
 $(document).ready(function () {
    $("#HotelDDL").change(function () {

        var text = $("#HotelDDL option:selected").text();
        var value = $("#HotelDDL option:selected").val();
        alert("Selected text=" + text + " Selected value= " + value);

        $.post("/Home/SearchForRooms",
         {
            HotelDDL: $("#HotelDDL option:selected").val() 
         }, function(result){//add callback for success - result is the returned view
            $("#successDiv").html(result);//place the view inside of the div
        });
    });
 });
</script>


コメント応答

@TunAntun-ビューに独自のページを持たせたい場合は、クラシック投稿を使用する必要があります。ajaxからこれを達成する方法はありません。あなたはjavascriptでこれを行うことができます

 $.post("/Home/SearchForRooms",
         {
            HotelDDL: $("#HotelDDL option:selected").val() 
         }, function(result){//add callback for success - result is the returned view
            $("#successDiv").html(result);//place the view inside of the div
        });

になります

        var postForm = document.createElement("form");
        postForm.setAttribute("method","post");
        postForm.setAttribute("action","/Home/SearchForRooms");
        var postVal = document.createElement("input");
        postVal.setAttribute("type","hidden");
        postVal.setAttribute("name","HotelDDL");
        postVal.setAttribute("value",$("#HotelDDL option:selected").val() );
        postForm.appendChild(postVal);
        document.body.appendChild(postForm);
        $(postForm).submit();

これにより、必要な投稿が動的に発行されます。次に、コントローラーで、ビューを適切にリダイレクトまたは返します。[HttpPost]使用するからビューを返すときは、更新によって古いデータが再投稿されないようにするためにRedirectToAction、メソッドからビューを返すことを強くお勧めします。[HttpGet]

于 2012-10-30T22:47:21.543 に答える