Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 MVC3 Html.BeginForm()和其他Html_Asp.net Mvc 3_Mvcextensions - Fatal编程技术网

Asp.net mvc 3 MVC3 Html.BeginForm()和其他Html

Asp.net mvc 3 MVC3 Html.BeginForm()和其他Html,asp.net-mvc-3,mvcextensions,Asp.net Mvc 3,Mvcextensions,我正在写一些代码,我想呈现一个MVC,但也包括额外的HTML 例如 @using (Html.BeginMyCustomForm()) { } 这个吐出来了 <form action="" etc> 加上 @Html.ValidationMessageFor(m=>m) 这可能吗? 我知道我会考虑某种扩展或最有可能的东西。 希望这是有意义的,并提前感谢 也许你可以用这样的东西 public static class FormExtensions { public

我正在写一些代码,我想呈现一个MVC,但也包括额外的HTML

例如

@using (Html.BeginMyCustomForm()) {

}
这个吐出来了

<form action="" etc>

加上


@Html.ValidationMessageFor(m=>m)
这可能吗?
我知道我会考虑某种扩展或最有可能的东西。

希望这是有意义的,并提前感谢

也许你可以用这样的东西

public static class FormExtensions
{
    public static MvcForm BeginMyCustomForm(this HtmlHelper htmlHelper)
    {
        var form = htmlHelper.BeginForm();
        htmlHelper.ViewContext.Writer.write( .... )
        return form;
    }
}

在BeginForm的扩展中包含这样的东西不是一个好主意。原因是好的设计要求对象做一件事。这被称为单一责任原则

现在,您希望表单对象也创建输入和验证

相反,使用MVC模板系统并创建一个默认模板以满足您的需要。您还应该使用Html.HiddenFor,而不是使用输入标记,除非有特定的理由不这样做

另一个选择是使用MVC脚手架生成您想要的内容

有关编辑器模板的详细信息


更多关于脚手架的信息

谢谢,这是一个全新的概念:-)太棒了!我让它也呈现一些文本。我会玩玩的,我不知道ViewContext.Writer。如果我能+1你,我会:-)
public static class FormExtensions
{
    public static MvcForm BeginMyCustomForm(this HtmlHelper htmlHelper)
    {
        var form = htmlHelper.BeginForm();
        htmlHelper.ViewContext.Writer.write( .... )
        return form;
    }
}