Ajax.ActionLink,得到控制器数据或错误信息? [英] Ajax.ActionLink, get data or error message from controller?

查看:239
本文介绍了Ajax.ActionLink,得到控制器数据或错误信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我想更新一个点:

 < D​​IV的风格=文本对齐:中心; ID =vote_count> @ Html.DisplayFor(Q => q.VoteCount)LT; / DIV>

下面是我的ActionLink的:

  @ Ajax.ActionLink(给予好评,给予好评,作者,新的{QuestionID = Model.QuestionID,@class =给予好评},
新AjaxOptions
{
     InsertionMode = InsertionMode.Replace,
     UpdateTargetId =vote_count
     OnBegin =onBegin
     的onComplete =的onComplete
     的onSuccess =的onSuccess
     onFailure处=onFailure处
})

这是我的控制器之一:

 公众诠释给予好评(GUID QuestionID)
{
    如果()
    {
        //我想发送错误信息
    }
    其他
    {
        //我想送一个整数
    }
}

我的问题:我要发送错误信息或整到我的视图页面来显示它。我该怎么办呢?
从已推荐意见,我可以改变所有codeS。

感谢。


解决方案

 公众的ActionResult给予好评(GUID QuestionID)
{
    如果(...)
    {
        返回的内容(一些错误信息);
    }
    其他
    {
        返回的内容(5票);
    }
}

不管你的文字在内容结果返回将被插入到DIV。

另一种可能性是使用JSON

 公众的ActionResult给予好评(GUID QuestionID)
{
    如果(...)
    {
        返回JSON(新{错误=一些错误信息},JsonRequestBehavior.AllowGet);
    }
    其他
    {
        返回JSON(新{票= 5},JsonRequestBehavior.AllowGet);
    }
}

和则:

  @ Ajax.ActionLink(给予好评,给予好评,作者,新的{QuestionID = Model.QuestionID,@class =给予好评},
新AjaxOptions
{
     的onSuccess =的onSuccess
})

和最后的onSuccess回调里面:

 函数的onSuccess(结果){
    如果(result.error){
        警报(result.error);
    }其他{
        $('#vote_count')HTML(result.votes)。
    }
}

Here is a spot that I want to update:

<div style="text-align: center;" id="vote_count">@Html.DisplayFor(q => q.VoteCount)</div>

Here is my actionLink:

@Ajax.ActionLink("Upvote", "Upvote", "Author", new { QuestionID = Model.QuestionID, @class = "upvote" },
new AjaxOptions
{
     InsertionMode = InsertionMode.Replace,
     UpdateTargetId = "vote_count",
     OnBegin = "onBegin",
     OnComplete = "onComplete",
     OnSuccess = "onSuccess",
     OnFailure = "onFailure"
})

And here is one of my controllers:

public int Upvote(Guid QuestionID)
{
    if ()
    {
        //I want to send error message
    }
    else 
    {
        //I want to send an integer
    }
}

And my question: I want to send error message or an integer to my view page to show it. How can I do it? From advices that you have recommended, I can change all codes.

Thanks.

解决方案

public ActionResult Upvote(Guid QuestionID)
{
    if (...)
    {
        return Content("some error message");
    }
    else 
    {
        return Content("5 votes");
    }
}

Whatever text you return in the content result will be inserted into the div.

Another possibility is to use JSON:

public ActionResult Upvote(Guid QuestionID)
{
    if (...)
    {
        return Json(new { error = "some error message" }, JsonRequestBehavior.AllowGet);
    }
    else 
    {
        return Json(new { votes = 5 }, JsonRequestBehavior.AllowGet);
    }
}

and then:

@Ajax.ActionLink("Upvote", "Upvote", "Author", new { QuestionID = Model.QuestionID, @class = "upvote" },
new AjaxOptions
{
     OnSuccess = "onSuccess"
})

and finally inside the onSuccess callback:

function onSuccess(result) {
    if (result.error) {
        alert(result.error);
    } else {
        $('#vote_count').html(result.votes);   
    }
}

这篇关于Ajax.ActionLink,得到控制器数据或错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆