繁体   English   中英

ASP.NET MVC表单问题

[英]ASP.NET MVC form issues

我正在尝试创建一个表单以将数据发送到我的控制器,但是我遇到了一些问题

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

我的表格:

@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" />
   }

另外,我正在使用引导程序来尝试并进行调整。

您可以尝试以下方法:

<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网格的更多信息。

暂无
暂无

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

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