如何返回文本助手从行动数据 [英] How to Return Helpers Text as data from Action

查看:216
本文介绍了如何返回文本助手从行动数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了下面的帮助(低于codeS只是证明我所期待的)

  @helper的SayHello(字符串文本)
{
   @文本
}

现在,从一个动作,我想为HTML(或字符串),如下

返回该助手的文字

 公众的ActionResult MyAction()
{
      //这样的事情只HTML返回
      返回的SayHello(入寺);
}


解决方案

我不知道这是可能的,但即便如此! - 请不要那样做这么多休息MVC的分离架构。

相反,实施,比如说在的SayHello 帮手,它是由一个操作方法被称为.cshtml文件(你应该装点您的操作方法在 [ChildActionOnly] 属性),然后通过使用你的剃须刀页面中调用该操作方法 @ Html.Action() @ Html.RenderAction()

I have created a following helper (below codes are just to demonstrate what I am looking for)

@helper SayHello(string text)
{
   @text
}

Now from an action i want to return this Helper's Text as Html (or string) as below

public ActionResult MyAction()
{
      //something like this to return just html
      return SayHello("Rusi");
}

解决方案

I don't know if this is possible, but even if so - PLEASE DO NOT DO THAT! It so much breaks MVC's separation architecture.

Instead, implement the SayHello helper in, say, a .cshtml file which is being called by an action method (you should decorate your action method in a [ChildActionOnly] attribute), and then you invoke the action method from within your Razor pages by using @Html.Action() or @Html.RenderAction().

这篇关于如何返回文本助手从行动数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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