ASP.. NET MVC表单问题

本文关键字:问题 表单 MVC NET ASP | 更新日期: 2023-09-27 18:03:22

我试图创建一个表单,以便获得数据到我的控制器,但我有一些问题

  1. (CSS)文本框不会在标签附近显示,而是在标签下方显示,如下图所示:http://prntscr.com/c7xlec

这是我唯一的额外的CSS形式:

.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() { }
}

My form:

@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来尝试和做对齐。

ASP.. NET MVC表单问题

你可以试试:

<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>