0

传回表单时,我的电阻器工作得很好,但是我构建的电阻器没有传回任何东西。我不确定我在做什么与 Visual Studio 提供的注册页面不同。它调用正确的 http post 方法,但传递的值为 null。谢谢你的帮助。我只是希望该模型回发到控制器操作结果。

控制器

[HttpGet]
        public ActionResult Support()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }
          [AllowAnonymous]
        [HttpPost, ValidateSpamPrevention]
        public ActionResult Support(QuickEmailModel email)
        {
            if (ModelState.IsValid)
            {
                 return View("thankyou", email);
            }
            return View(email);

        }

模型

public class QuickEmailModel
{
    [DataType(DataType.EmailAddress)]
    [EmailAddress]
    public string Email { get; set; }

    [Required]
    public string Subject { get; set; }

    [Required]
    [DataType(DataType.MultilineText)]
    public string Description { get; set; }

    [Display(Name = "Company (Optional):")]
    public string Company { get; set; }
}

}

视图顶部

@using PoliteCaptcha
@model Models.QuickEmailModel
@{
    ViewBag.Title = "Support";
}

表格所在的底部

 @using (Html.BeginForm())
                {
                    @Html.ValidationSummary()
                    <div class="control-group">
                        <label class="control-label">
                            @Html.LabelFor(x => x.Company, "Company (Optional):", new { @class = "control-label" })</label>
                        <div class="controls">
                            @Html.TextBoxFor(x => x.Company, new { @class = "span4" })
                        </div>
                    </div>
                    <div class="control-group">
                        @Html.LabelFor(x => x.Email, "Email:", new { @class = "control-label" })
                        <div class="controls">
                            @Html.TextBoxFor(x => x.Email, new { @Class = "span4" })
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">
                            @Html.LabelFor(x => x.Subject, "Subject:", new { @class = "control-label" })</label>
                        <div class="controls">
                            @Html.TextBoxFor(x => x.Subject, new { @class = "span4" })
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">
                            @Html.LabelFor(x => x.Subject, "Description:", new { @class = "control-label" })</label>
                        <div class="controls">
                            @Html.TextAreaFor(x => x.Description, new { @class = "span4", @rows = "6", id = "txtDescription" })
                        </div>
                    </div>
                    @Html.SpamPreventionFields()
                    <input type="submit" class="btn btn-success" id="btnSubmit" value="Submit" style="margin-right: 15em;
                        float: right;" />
                }
4

2 回答 2

3

我认为问题在于您的参数名称与您的字段名称之一(电子邮件)相同。将方法参数名称重命名为模型(或电子邮件以外的其他名称)。

我认为正在发生的事情是 MVC 模型绑定器看到一个名为“email”的发布值,它试图将其分配给同名的参数,但由于它不是一个字符串,所以它不能这样做。因此它分配一个空值。

此外,模型绑定不区分大小写,因此电子邮件和电子邮件是相同的。

于 2012-09-27T22:01:33.470 回答
0

问题是模型映射器无法将表单名称/值对从 POST 请求映射到 QuickEmailModel 对象的实例,而只是忽略它们。检查在 Firebug 或其他工具中发送的 POST 请求,名称/值对的名称是什么?

您可能应该像这样更改您的视图:

@{ var email = ....; }

<label class="control-label">
    @Html.LabelFor(x => email.Company, "Company (Optional):", new { @class = "control-label" })
</label>
<div class="controls">
    @Html.TextBoxFor(x => email.Company, new { @class = "span4" })
</div>

因为您的 POST 操作参数的名称是“电子邮件”。我认为这应该强制表单参数名称为“email.Company”等。如果不是,请尝试其他变体

@Html.TextBox("email.Company", model.Company)
于 2012-09-27T21:46:01.680 回答