在自定义HTML帮助器中获取MvcHtmlString以获取操作 - c#

我正在尝试构建一个自定义html帮助器,该显示器将基于传递给该html帮助器的操作显示动态内容,这是到目前为止的内容

// the Area helper class
public static class AreaHelper
{
      public static AreaBuilder CustomArea(this HtmlHelper helper)
      {
           return new AreaBuilder();
      }
}
// the area builder class
public class AreaBuilder: IHtmlString
{
    private string name;
    private string caption;
    private string action;
    private string controller;
    private object routeData;

    public AreaBuilder Name(string name)
    {
        this.name=name;
        return this;
    }

    public AreaBuilder Action(string action,string controller,object routeData)
    {
        this.action=action;
        this.controller=controller;
        this.routeData=routeData;
        return this;
    }

    public string ToHtmlString()
    {
         var html=new StringBuilder();
         html.AppendFormat(@"<div id=""{0}"" class=""area"">",name);
         html.AppendFormat(@"<div class=""area-caption"">{0}</div>",caption);
         html.AppendFormat(@"<div class=""area-content"">{0}</div>",**what should i do here**);
         html.Append("</div>");
         return html.ToString();
    }
}

我无法执行正确的步骤以获取我拥有的URL的ActionResult内容(action,controller,routeData)

换句话说,我如何以mvchtmlstring的形式获得Action?

任何帮助表示赞赏

参考方案

您可以使用以下内容

解决方案一

Add reference to System.Web.Mvc.Html;
ChildActionExtensions.Action(YourHtmlHelper, action,controller,routeData);

所以你的代码将变得像

// the area builder class
public class AreaBuilder: IHtmlString
{
    private string name;
    private string caption;
    private string action;
    private string controller;
    private object routeData;
    private HtmlHelper htmlHelper;

    public AreaBuilder Name(string name)
    {
        this.name=name;
        return this;
    }

    public AreaBuilder HtmlHelper(HtmlHelper helper)
    {
        this.htmlHelper=helper;
        return this;
    }

    public AreaBuilder Action(string action,string controller,object routeData)
    {
        this.action=action;
        this.controller=controller;
        this.routeData=routeData;
        return this;
    }

    public string ToHtmlString()
    {
         var html=new StringBuilder();
         html.AppendFormat(@"<div id=""{0}"" class=""area"">",name);
         html.AppendFormat(@"<div class=""area-caption"">{0}</div>",name);
         html.AppendFormat(@"<div class=""area-content"">{0}</div>",ChildActionExtensions.Action(htmlHelper, action,controller,routeData));
         html.Append("</div>");
         return html.ToString();
    }
}

和您的区域HTML帮助器

// the Area helper class
public static class AreaHelper
{
      public static AreaBuilder CustomArea(this HtmlHelper helper)
      {
           return new AreaBuilder().HtmlHelper(helper);
      }
}

解决方案二

还有另一种方法可以实现您想要的,您可以执行以下操作

// the area builder class
public class AreaBuilder: IHtmlString
{
    private string name;
    private string caption;
    private MvcHtmlString content; // to allow the use to add html content

    public AreaBuilder Name(string name)
    {
        this.name=name;
        return this;
    }

    public AreaBuilder Content(Func<object, HelperResult> html)
    {
        var detail=html.Invoke(null);
        this.content=MvcHtmlString.Create(detail.ToHtmlString());
        return this;
    }

    public string ToHtmlString()
    {
         var html=new StringBuilder();
         html.AppendFormat(@"<div id=""{0}"" class=""area"">",name);
         html.AppendFormat(@"<div class=""area-caption"">{0}</div>",caption);
         html.AppendFormat(@"<div class=""area-content"">{0}</div>",content.ToHtmlString());
         html.Append("</div>");
         return html.ToString();
    }
}

并且在您的代码中,即使我认为名称应为CustomArea而不是CustomButton,您仍应执行以下操作。

@Html.CustomArea().Name("MyArea").Content(@<text>@Html.Action("YourAction","YourController",...your routing data)</text>)

希望这个能对您有所帮助

HTML Action ID获取方法 - javascript

这可能已经是另一个问题的副本,但我找不到它。我采取了以下行动:<form id="bug-form" action="/AST_14/Bugg.ly2/index.php/bug/update/" method="post"> 现在,我的系统的工作方式是每个需要更新的“错误”都有自己的错…

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

故障排除“警告:session_start():无法发送会话高速缓存限制器-标头已发送” - php

我收到警告:session_start()[function.session-start]:无法发送会话缓存限制器-标头已发送(错误输出开始如果我将表单数据提交到其他文件进行处理,则可以正常工作。但是,如果我将表单数据提交到同一页面,则会出现此错误。请建议<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0…

CodeIgniter更新查询被执行两次 - php

我正在使用CodeIgniter 2.2。每次访问页面时,我都必须用+1更新数据库。代码可以工作,但是每次都会增加+2。示例:如果是total views=2,则在单击页面后total views应该是3,但是数据库中的值是4。我确定我在控制器中仅调用一次模型add_one_to_view_image。控制者 function view(){ $view_i…

jQuery Ajax文件上传在客户端浏览器上无法正常工作 - javascript

我正在尝试使用Ajax和JQuery实现个人资料图片上传功能我能够将个人资料图片成功上传到我尝试过的所有机器和移动设备上的数据库中。它适用于我在Chrome,Edge,Firefox,Safari甚至Vivaldi上使用。问题是我住在加拿大的客户无法将他们的个人资料图片上传到数据库。另外,纵向宽高比的图像倾向于向侧面旋转。我一直在要求他重​​新注册并多次提供…