ASP.NET MVC:值到达Controller时为null

时间:2013-11-07 17:59:02

标签: asp.net-mvc

所以我有以下控制器:

    [HttpPost]
    public ActionResult CreateSupport(CreateSupport model)
    {
        if (ModelState.IsValid && (model.Description != null))
        {
            model.CreatedById = UserId;
            model.ModifiedById = UserId;
        }

        return View(model);
    }

我有以下观点:

@using (Html.BeginForm("CreateSupport", "Support", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend></legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Subject, new Dictionary<string, object>() { { "class", "req" } })
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.Subject)
            @Html.ValidationMessageFor(model => model.Subject)
        </div>

        <div class="support-form-left">

            <div class="editor-label">
                @Html.LabelFor(model => model.BrowserInfo, new Dictionary<string, object>() { { "class", "req" } })
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.BrowserInfo)
                @Html.ValidationMessageFor(model => model.BrowserInfo)
            </div>
        </div>

        <div class="support-form-right">

            <div class="editor-label">
                @Html.LabelFor(model => model.DatabaseVersion, new Dictionary<string, object>() { { "class", "req" } })
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.DatabaseVersion)
                @Html.ValidationMessageFor(model => model.DatabaseVersion)
            </div>

        </div>

        <div class="clearFloat"></div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description, new Dictionary<string, object>() { { "class", "req" } })
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <div class="actionButtons">
            <button id="btnCancel" class="myButtonCancel">Cancel</button>
            <input type="submit" value="Submit" class="myButton" />
        </div>
        @if (ViewBag.SuccessMessage != null)
        {
            <div>
                <label style="color: red;">@ViewBag.SuccessMessage</label>
            </div>
        }
    </fieldset>

}

这是模型:

public class CreateSupport : SupportTicket
{
    public CreateSupport()
    {
        ProductList = new List<Product>();
        ProductVersionsList = new List<ProductVersion>();
        EnviromentList = new List<InstallationEnvironment>();
        content = new Content();
    }

    [Required]
    [UIHint("tinymce_jquery_full"), AllowHtml]
    public string Description { get; set; }

    [Required]
    [DisplayName("Browser version Info.")]
    public string BrowserInfo { get; set; }

    [Required]
    [DisplayName("Database Version")]
    public string DatabaseVersion { get; set; }

    public Content content { get; set; }

}

问题是,即使您在其中输入了一些值,到达Controller的值也是NULL。

2 个答案:

答案 0 :(得分:1)

您应该检查浏览器的开发人员工具,看看表单是否正确发布了其值。如果不是,你应该做两件事:

A)禁用javascript以查看是否存在干扰POST的脚本(通常是通过禁用或清除字段)

B)使用W3C标记验证服务确保您的标记有效

答案 1 :(得分:0)

对于输入字段,请使用

@Html.EditorFor(x => x.Subject)

对于显示字段,请使用

@Html.DisplayFor(x => x.Subject)