2

私はかなり新しいですMVC。を使用してwithパラメータをajax呼び出す必要があります。私はこれを渡すことができません。Actionhtml.Action()

他のMVC初心者にも役立つことを願っています。

HTML:

<%: Html.ActionLink("Add Race", "AddRace", 
        new {eventId = Model.EventId, fleetId=Model.SelectedFleet.ID}, 
        new{@onclick=string.Format("return checkFleetAddedandScroing()")}) %>

Jquery:

 function checkFleetAddedandScroing() {
        debugger;
        $.ajax({
            type: "GET",
            url: '<%=Url.Action("CheckFleetExists")%>',
            dataType: "json",
            cache: false,
            success: function (data, textStatus) {
                data = eval("(" + data + ")");
                if (data == true) {
                    return true;
                }
                else {
                    alert("Cannot Add race becasue you have not yet added any fleets and fleet scoring is checked.");
                    return false;
                }
            }, //success
            error: function (req) {

            }
        });           
    }

アクション:

  public JsonResult CheckFleetExists(Guid fleetId )
    {
        bool exists = false;
        try
        {
            exists = !db.Races.Any(r => r.FleetID == fleetId);
        }
        catch
        {
        }
        return Json(exists, JsonRequestBehavior.AllowGet);
    }

fleetidにあるアクションに渡す必要がありModel.SelectedFleet.IDます。ページのどこかで使用されています。しかし、どういうわけかそれを使用することはできません。

私が間違っているところを提案してください...

4

3 に答える 3

4

リンクがクリックされたときにAJAXを使用してコントローラーアクションを呼び出そうとしているようです。この呼び出しの結果に応じて、ユーザーを実際のAddRaceアクションにリダイレクトするか、エラーメッセージを表示するように求められます。

コードの問題は、成功したAJAXコールバック内からtrue / falseを返そうとしていることですが、これは意味がありません。サーバーからの戻り値に応じて、クリックコールバックから常にfalseを返し、成功コールバック内でwindow.location.href関数を使用して手動でリダイレクトする必要があります。

HTML:

<%: Html.ActionLink(
    "Add Race", 
    "AddRace", 
    new {
        eventId = Model.EventId, 
        fleetId = Model.SelectedFleet.ID
    }, 
    new {
        data_fleetid = Model.SelectedFleet.ID,
        @class = "addRace"
    }
) %>

Jquery:

<script type="text/javascript">
    $(function () {
        $('.addRace').click(function (evt) {
            $.ajax({
                type: 'GET',
                url: '<%= Url.Action("CheckFleetExists") %>',
                cache: false,
                data: { fleetId: $(this).data('fleetid') },
                success: function (data) {
                    if (data.exists) {
                        // the controller action returned true => we can redirect
                        // to the original url:
                        window.location.href = url;
                    }
                    else {
                        alert("Cannot Add race becasue you have not yet added any fleets and fleet scoring is checked.");
                    }
                },
                error: function (req) {

                }
            });

            // we make sure to cancel the default action of the link
            // because we will be sending an AJAX call
            return false;
        });
    });
</script>

アクション:

public ActionResult CheckFleetExists(Guid fleetId)
{
    bool exists = false;
    try
    {
        exists = !db.Races.Any(r => r.FleetID == fleetId);
    }
    catch
    {
    }
    return Json(new { exists = exists }, JsonRequestBehavior.AllowGet);
}

備考:AddRaceコントローラーアクション内では、内で実行しているのと同じ検証を実行することを忘れないでくださいCheckFleetExists。ユーザーはJavaScriptを無効にするだけで、AJAX呼び出しは実行されません。

于 2012-10-27T15:20:44.167 に答える
1

このようにあなたの行動を変えてください:

 public JsonResult CheckFleetExists(string fleetId)
    {
        var fleetGuid = Guid.Parse(fleetId);
        bool exists = false;
        try
        {
            exists = !db.Races.Any(r => r.FleetID == fleetGuid );
        }
        catch
        {
        }
        return new JsonResult{ Data = exists};
    }

ActionLinkを変更してください:

<%: Html.ActionLink("Add Race", "AddRace", 
    new {eventId = Model.EventId, fleetId=Model.SelectedFleet.ID}, 
    new{onclick=string.Format("return checkFleetAddedandScroing({0})",Model.SelectedFleet.ID)}) %>

スクリプトブロックは次のようになります。

function checkFleetAddedandScroing($fleetId) {
        $.ajax({
            type: "POST",
            url: '<%=Url.Action("CheckFleetExists")%>',
            dataType: "json",
            data : { "fleetId" : $fleetId },
            cache: false,
            success: function (data, textStatus) {
                data = eval("(" + data + ")");
                if (data == true) {
                    return true;
                }
                else {
                    alert("Cannot Add race becasue you have not yet added any fleets and fleet scoring is checked.");
                    return false;
                }
            }, //success
            error: function (req) {

            }
        });           
    }
于 2012-10-27T12:52:36.613 に答える
1

問題は、アクションへのURLが完全ではなかったという回答にありました。私はこの方法を使用してそれを行いました

function checkFleetAddedandScroing() {
       // debugger;
        $.ajax({
            type: "POST",
            url: '<%=Url.Action("CheckFleetExists", new {eventId=Model.EventId})%>',
            dataType: "json",                
            cache: false,
            success: function (data, textStatus) {
                data = eval("(" + data + ")");
                if (data == true) {
                    return true;
                }
                else {
                    alert("Cannot Add race becasue you have not yet added any fleets and fleet scoring is checked.");
                    return false;
                }
            }, //success
            error: function (req) {

            }
        });
    }
于 2012-10-28T14:06:47.430 に答える