Why not all form controls can be displayed through HtmlHelper?

Does anyone know why some HTML form controls can be displayed using System.Web.Mvc.HtmlHelper (hidden, checkbox, password, text field), and some can't be written explicitly in HTML (file, send)? What is the principle of this separation?

+3
source share
2 answers

Good question.

I would think that the input controls (like input, select) are wrapped in HTML helpers so that they can maintain their own state through TempData without having to write a lot of code to achieve this. I also think that the link and form controls are wrapped to give a simple and consistent way to specify the controller / action for these controls. Most other controls do not guarantee either out-of-box status control or the need to create control / actions.

Of course, there is nothing that would prevent you from writing your own wrappers for what you need - for example, here is a couple that I use - here is one for managing the html tag:

public static string Label(this HtmlHelper helper, string fieldName, string labelText)
{
    var sb = new StringBuilder();
    sb.Append("<label for=\"");
    sb.Append(fieldName);
    sb.Append("\">");
    sb.Append(labelText);
    sb.Append("</label>");
    return sb.ToString();
}

And here is the one I use that wraps this label helper and text field to create a single text input field with a label:

public static string TextField(this HtmlHelper helper, string labelText, string fieldName, string value)
{
    return string.Concat(
        "<div>",
        helper.Label(fieldName, labelText),
        helper.TextBox(fieldName, value),
        "</div>");
}
+4

"Html.SubmitButton() Preview4?" :

, . , . " HTML, SubmitButton?".

, , , . , VS HTML , intellisense, . . .

...

, html. : Html.Div.

. , , , . , SubmitButton.

- form helper, , , info, ...

+3

Source: https://habr.com/ru/post/1706869/


All Articles