我正在尝试创建一个表单以便将数据提供给我的控制器,但是我遇到了一些问题
这是我唯一的格式:
.textbox-style{
padding-left: 50px;
}
.validation-style{
color: red;
}
2。 (MVC)
当我使用空字段提交数据时,我不会收到我在模型中设置的错误消息。
型号:
public class EmailModel
{
[Required(ErrorMessage = "Name Required")]
public string Name { get; set; }
[Required(ErrorMessage = "Message Required")]
public string Message { get; set; }
[Required(ErrorMessage = "Email Required")]
[DataType(DataType.EmailAddress)]
[EmailAddress]
public string Email { get; set; }
public EmailModel(string Name, string Message, string Email)
{
this.Email = Email;
this.Message = Message;
this.Name = Name;
}
public EmailModel() { }
}
我的表格:
@model ForIT2016.Models.EmailModel
<div id="feedback-form" class="form-horizontal">
@using (Html.BeginForm())
{
<div class="form-group">
@Html.LabelFor(m => m.Name, "•Name")
@Html.TextBoxFor(m => m.Name, null, new { @class = "form-control textbox-style" })
@Html.ValidationMessageFor(m => m.Name, null, new { @class = "validation-style" })
</div>
<br />
<div class="form-group">
@Html.LabelFor(m => m.Email, "•Email")
@Html.TextBoxFor(m => m.Name, null, new { @class = "form-control textbox-style" })
@Html.ValidationMessageFor(m => m.Name, null, new { @class = "validation-style" })
</div>
<br />
<div class="form-group">
@Html.LabelFor(m => m.Message, "•Message")
@Html.TextAreaFor(m => m.Name, new { @class = "form-control textbox-style", style = "height:100px; width:250px;" })
@Html.ValidationMessageFor(m => m.Name, null, new { @class = "validation-style" })
</div>
<br />
<input type="submit" class="btn btn-primary" value="Send" />
}
另外,我正在使用bootstrap尝试做对齐。
答案 0 :(得分:0)
你可以试试这个:
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2")
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control"} })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "validation-style" })
</div>
</div>
而不是:
<div class="form-group">
@Html.LabelFor(m => m.Name, "•Name")
@Html.TextBoxFor(m => m.Name, null, new { @class = "form-control textbox-style" })
@Html.ValidationMessageFor(m => m.Name, null, new { @class = "validation-style" })
</div>
详细了解Bootstrap网格here。