简体   繁体   English

替换Ajax.ActionLink

[英]Replace Ajax.ActionLink

Hi I have an Ajax Action Link that works perfectly and it shows some text from a partial view once I click on the link, the thing is I want these texts to be shown once the page loads without clicking the action link. 嗨,我有一个Ajax动作链接,它可以正常工作,单击链接后它会从局部视图中显示一些文本,问题是我希望这些页面加载后立即显示这些文本,而无需单击动作链接。 I'm new to this and I've already searched on google but nothing helps. 我对此并不陌生,已经在Google上进行了搜索,但没有帮助。

Here's the code 这是代码

View.cshtml View.cshtml

@Ajax.ActionLink("Show my Text", "_Text",
new AjaxOptions
                {
                    HttpMethod = "GET", 
                    UpdateTargetId = "ajaxdiv",
                    InsertionMode = InsertionMode.Replace 
                })

 <div id="ajaxdiv"></div>

Partial View: _Text.cshtml 局部视图:_Text.cshtml

@model Website.MyViewModel

@foreach (TextTable item in Model.TextList)
{
    <h5> @item.UserText </h5>  
}

Controller: 控制器:

public PartialViewResult  _Text()
    {

        DBEntities DB = new DBEntities();
        var sessionID = Session["id"];
        int SessionID = Convert.ToInt32(sessionID);
        var viewModel = new MyViewModel
        {

            TextList= DB.TextTable.ToList()
        };

        return PartialView(viewModel);
    }

Add OnSuccess in your ajax options and make jquery function that will hide it. 在您的ajax选项中添加OnSuccess并使用jquery函数将其隐藏。

@Ajax.ActionLink("Show my Text", "_Text",
new AjaxOptions
                {
                    HttpMethod = "GET", 
                    UpdateTargetId = "ajaxdiv",
                    OnSuccess = "hideFunction",
                    InsertionMode = InsertionMode.Replace 
                })

In your js make a function like. 在您的js中创建一个类似的函数。

function hideFunction() {
    $(this).hide();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM