我的HTML Button-Helper类 [英] My HTML Button-Helper class

查看:85
本文介绍了我的HTML Button-Helper类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

Hey guys.

My HTML Button-Helper class is:

public static class ButtonHelpers
  {
public static MvcHtmlString Firstbutton (this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
    {
      var builder = new TagBuilder("button");
      builder.AddCssClass("mybutton");
      builder.InnerHtml = linkText;
      //builder.MergeAttributes(htmlAttributes);
      return MvcHtmlString.Create(builder.ToString());  
    }
}


我的HTML代码视图是:(不是很好的代码=错误的代码)


My View Html-code is: (Not really good codes = wrong Code)

<div class=" mybutton ">
    <div class=" mybutton left"></div>
    <div class=" mybutton middle">
    @Html.Firstbutton("Send ", "Marketing", "Finance", new{}, new { @class = " mybutton " })</div>
    <div class=" mybutton right"></div>
    </div>

        <span>|</span>

    <div class=" mybutton ">
    <div class=" mybutton left"></div>
    <div class=" mybutton middle">
    @Html. Secondbutton ("Back", "Marketing", "Finance", new { ID = Model. Marketing.ID },  
    new { @class = " mybutton " })</div>
    <div class=" mybutton right"></div>
    </div>



实际上每个按钮都有3个部分(左中右).
实施后,我得到了两个彼此靠近的好按钮.
结果还可以,我没有出现任何错误,但这不是我想要的.
这些按钮实际上应该使用Razor中的Html代码以下创建:
正确的代码:



Every button has in fact 3 parts (left-middle-right).
After implementing I got 2 good buttons near to each other.
The result was okay and I got no errors but it wasn’t what I wanted.
These buttons actually should be created with below Html-code in Razor:
Correct code:

<div>
 @Html.Firstbutton("Send ", "Marketing", "Finance", new{}, new { @class = " mybutton " })</div>
<span>|</span>
<div>
@Html. Secondbutton ("Back", "Marketing", "Finance", new { ID = Model. Marketing.ID }, new { @class = " mybutton " })</div>


我想我应该在我的HTML-Helper类中编写一些代码,例如:


I think I should write some code in My HTML-Helper class like:

var builder = new TagBuilder("div");
var innerBuilder = new TagBuilder("a");
builder.InnerHtml = innerBuilder.ToString();
return MvcHtmlString.Create(builder.ToString());


有人建议吗?

谢谢,


Anybody suggestion?

Thanks,

推荐答案

我已经回答了同一主题的其他主题:是的,您看对了.如果您希望由助手来构建compex html代码,则需要完全按照注释末尾的内容进行操作:
您可以使用具有另一个tagbuilder的结果作为InnerHtml的tagbuilder.这就是方法.
您可以使用以下方法来构建A标签:

As I have allready answered to you other thread in the same topic: yes, you see it right. If you want compex html code to be built by your helper, you need to do exactly what you have included at the end of your comment:
you can use tagbuilders that have as InnerHtml the result of another tagbuilder. This is the way.
Here is how you can use your parameters to build the A tag:

var anchorBuilder = new TagBuilder("a");
anchorBuilder.MergeAttribute("href", url.Action(actionName, controllerName, routeValues));
anchorBuilder.MergeAttribute("title", linkText);
anchorBuilder.MergeAttributes(htmlAttributes);



我想您可以找出其余的帮助程序.



I think you can figure out the rest of the helper.


Thanks for your reply en your help.
I changed the codes like here, but still new problems:


public static MvcHtmlString Button(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, IDictionary<string, string> htmlAttributes)
    {
      // build the <div> tag
      TagBuilder divTag = new TagBuilder("div");
      divTag.AddCssClass("mybutton"); // mybutton for the outer div

      // build the <a> tag
      TagBuilder anchorTag = new TagBuilder("a");
      anchorTag.AddCssClass("middle");   // left middle and right for the inner elements
      anchorTag.MergeAttribute("href", url.Action(actionName, controllerName, routeValues));
      anchorTag.MergeAttribute("title", linkText);
      anchorTag.MergeAttributes(htmlAttributes);

      // compose tags
      divTag.InnerHtml = anchorTag.ToString();
      return MvcHtmlString.Create(divTag.ToString());
    }


In Razor I wrote:


<div>
    @Html.Button("Back", "Marketing", "Finance", new { ID = Model. Marketing.ID }, null)
</div


First problem:
I had red-line error under ‘url’
(the name’url’ does not exist in the current context)

I can solve this problem with "this UrlHelper url". But then I should write:
"this HtmlHelper htmlHelper, UrlHelper url". En I know that is wrong!

Second problem: 
I think I should change my @Html.Button  in Razor. 
But I don’t know how?

Bye the way, how can I delete a question in "Code Project"??


这篇关于我的HTML Button-Helper类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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