c# - ASP.NET MVC 表单问题

标签 c# css asp.net-mvc forms twitter-bootstrap

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

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

此外,我正在使用 bootstrap 来尝试进行对齐。

最佳答案

你可以试试这个:

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

关于c# - ASP.NET MVC 表单问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39048679/

相关文章:

C# NetSuite Web 服务 : Get value from custom field in saved search (ItemSearchAdvanced)

c# - 根据十六进制值从字符串中删除特定字符

html - 基于 vue.js 变量中字符串的类名

html - HTML 颜色中的 CSS

css - 如何对齐两个字段

c# - 如何在不违反 MVC 模式的情况下实现缓存模型?

c# - 使用 C# 应用程序在 Twitter 中发帖

c# - 隐藏函数

javascript - 如何更改 Bootstrap 中字形的内部颜色?

asp.net-mvc - 在 Controller 中设置用户角色?