简体   繁体   English

如何在ASP.NET MVC 5中从模型创建表单?

[英]How to create a form from a model in ASP.NET MVC 5?

Is there a built in function that basically takes an object parameter from a model and creates a complete form based on that? 是否有内置函数基本上从模型中获取对象参数并基于此创建完整的表单?

Currently I'm doing a line for each property: 目前我正在为每个房产做一行:

@model AutomatedTellerMachine.Models.ContactFormModel
@using (Html.BeginForm())
{
    <div class="form-horizontal">

        <div class="form-group">
            <div class="col-md-10">
                <input type="text" name="name" class="form-control" />
                @Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-10">
                <input type="text" name="phone" class="form-control" />
                @Html.ValidationMessageFor(model => model.phone, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-10">
                <textarea name="message" class="form-control"></textarea>
                @Html.ValidationMessageFor(model => model.message, "", new { @class = "text-danger"})
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-10">
                <input type="submit" value="Send" class="btn btn-default"/>
            </div>
        </div>
    </div>

}

I realize you can get Visual Studio to create everything for you, but I need to mix and match. 我意识到你可以让Visual Studio为你创造一切,但我需要混合搭配。

You can do this with Editor Templates. 您可以使用编辑器模板执行此操作。 Create a new Razor view named ContactFormModel.cshtml (the name must match the model). 创建一个名为ContactFormModel.cshtml的新Razor视图(名称必须与模型匹配)。 Inside that file, copy all of the code from your existing view. 在该文件中,复制现有视图中的所有代码。

You can use normal Razor syntax to render whatever HTML that you want to include when calling EditorFor() on this model. 在此模型上调用EditorFor()时,您可以使用普通的Razor语法来呈现要包含的HTML。

Then, you can have Razor render your custom template with: 然后,您可以让Razor渲染您的自定义模板:

@Html.EditorForModel(Model)

If you want to do the same for a specific object (eg, YourObjectName ) in the model, rather than the whole thing, create a file named YourObjectName.cshtml . 如果要对模型中的特定对象(例如, YourObjectName )执行相同操作(而不是整个操作),请创建名为YourObjectName.cshtml的文件。

At the top of this file: 在这个文件的顶部:

@model NameSpace.YourObjectName

Then use this in the outer view that you want to render the custom Editor Template in: 然后在要在以下位置呈现自定义编辑器模板的外部视图中使用它:

@Html.EditorFor(model => model.YourObjectPropertyName)

Ok so I figured it out, using: 好的,所以我想出来了,使用:

@Html.EditorForModel(Model)

works, but is there a way to assign a class to all elements that it will create? 有效,但有没有办法将类分配给它将创建的所有元素?

May be you are looking for this. 也许你正在寻找这个。

 @Html.EditorFor(model => model, new { htmlAttributes = new { @class = " form-control" } }) 

The 'form-control' class is assigned to input fields. 'form-control'类被分配给输入字段。 It will generate this html for each property 它将为每个属性生成此html

 <div class="editor-label"><label for="UserName">User Name</label></div> <div class="editor-field"> <input class=" form-control text-box single-line" data-val="true" data-val-length="The field User Name must be a string with a maximum length of 50." data-val-length-max="50" data-val-required="The User Name field is required." id="UserName" name="UserName" type="text" value=""> <span class="field-validation-valid" data-valmsg-for="UserName" data-valmsg-replace="true"></span> </div> 

The problem with this is 这个问题是

  1. It will generate label and input in separate lines. 它将在单独的行中生成标签和输入。
  2. Label and input fields are not grouped under 'form-group' div. 标签和输入字段不在“form-group”div下分组。 which makes it difficult to customize the styles. 这使得定制样式变得困难。

But there is always a work around. 但总有一种解决方法。 I have once styled a form with labels aligned on left and inputs aligned on right with different background-color for labels area and inputs area. 我曾经设计过一个表格,左边是标签,右边是输入,标签区域和输入区域有不同的背景颜色。 Here is how you can do it if anybody is interested. 如果有人感兴趣的话,你可以这样做。

First of all, name your form with something so your styles does not effect other forms. 首先,为您的表单命名,以便您的样式不会影响其他表单。

 #RegisterForm > .editor-label { float: left; width: 250px; background-color: lightblue; box-shadow: 0px 35px lightblue; padding-top: 10px; } #RegisterForm > .editor-label > label { float: right; padding-right: 5px; } #RegisterForm > .editor-field { margin-left:250px; background-color: yellowgreen; margin-bottom: 15px; box-shadow: 0px 15px yellowgreen; padding-left: 5px; padding-right: 20px; } 

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM