Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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
C# 如何正确地将html绑定到模型数据_C#_Asp.net_.net_Asp.net Mvc_Model View Controller - Fatal编程技术网

C# 如何正确地将html绑定到模型数据

C# 如何正确地将html绑定到模型数据,c#,asp.net,.net,asp.net-mvc,model-view-controller,C#,Asp.net,.net,Asp.net Mvc,Model View Controller,因此,我正在使用ASP.NETMVC开发我的第一个网页,并且我设法创建了一个完全工作的注册页面,该页面将数据发送到数据库并存储用户。简单 然而,我并不真的喜欢它为我创造的元素的外观和感觉,所以我想我可以改变它 原始代码正常工作 <div class="form-group"> @Html.LabelFor(model => model.Firstname, htmlAttributes: new { @class = "control-l

因此,我正在使用ASP.NETMVC开发我的第一个网页,并且我设法创建了一个完全工作的注册页面,该页面将数据发送到数据库并存储用户。简单

然而,我并不真的喜欢它为我创造的元素的外观和感觉,所以我想我可以改变它

原始代码正常工作

<div class="form-group">
                    @Html.LabelFor(model => model.Firstname, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.Firstname, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Firstname, "", new { @class = "text-danger" })
                    </div>
                </div>
<div class="row">
                <div class="input-group bb-none">
                    <i class="fas fa-address-card"></i>
                    <input type="text" placeholder="Firstname">
                </div>
            </div>

@LabelFor(model=>model.Firstname,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Firstname,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Firstname,“,new{@class=“text danger”})
我的新代码不起作用

<div class="form-group">
                    @Html.LabelFor(model => model.Firstname, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.Firstname, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Firstname, "", new { @class = "text-danger" })
                    </div>
                </div>
<div class="row">
                <div class="input-group bb-none">
                    <i class="fas fa-address-card"></i>
                    <input type="text" placeholder="Firstname">
                </div>
            </div>

我99.9%确信这是一个有约束力的问题。我想把数据输入我的新文本框

<input type="text" placeholder="Firstname">

将数据传递到模型


在第一个选项中绑定它的部分是什么?

标记帮助程序将解析为html,并将属性名称作为id和名称放在输入中。然后,模型绑定器将绑定到该模型

  @Html.TextBoxFor( m => m.Firstname, new { placeholder = "Firstname" })

如果在工作代码中检查HTML helper呈现的HTML标记,您会注意到它添加了表单值发送中使用的名称属性“Firstname”,并绑定到处理表单提交的操作方法的参数。所以只需添加name=“Firstname”就可以让输入为表单提交增加价值。可能答案是@AngelD-只是在添加注释时击败了我。我认为这将是model.Firstname不幸的是,这不起作用,
asp for
甚至不会在intellisense中弹出,不确定它是否应该弹出,但它仍然会给出一个无效的请求,当Firstname丢失时会发生这种情况对不起,我的坏消息是在考虑.net core。以上更新。EditorFor也不错,它还有一个额外的好处,就是根据int的model type=“text”或type=“number”设置输入类型。因此,我应该使用
@Html.TextBox(“Firstname”)
而不是
?我不知道它是如何绑定的。如果我使用它,我将如何添加占位符标记?这将是一个很好的参考。我用“Firstname”给出的第一个示例是松散类型的,这意味着我可以在其中添加任何内容,现在我已经更新了一个更强类型的示例,它将使用模型中的Firstname和占位符。此链接有助于解释这些输入字段等