2

我很陌生MVC。我需要ajax使用. 这个我过不去。。Actionhtml.Action()

希望它对其他 MVC 初学者也有帮助..

HTML:

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

查询:

 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);
    }

我需要传递fleetidModel.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"
    }
) %>

查询:

<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

问题在于行动的网址不完整的答案..我用这种方式做到了

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 回答