Asp.net mvc 3 MVC3联系人表单在发布后不发送电子邮件

Asp.net mvc 3 MVC3联系人表单在发布后不发送电子邮件,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,我构建了一个简单的联系人表单,在我的测试“mvc1application”中,它在本地非常有效。当我把准确的代码转移到我的一个网站上,在那里我会得到这个表格,我会遇到问题,真的很难办 场景是当用户访问站点时,它会检查站点是否已通过身份验证(通过SSO系统)。如果是这样,则设置cookie并登录。如果您尝试在未登录的情况下访问该站点,则当前会被重定向到“AccessDenied.cshtml”视图。那没问题 此后,我用一个表单(简单的联系表单)取代了这个视图,用户可以填写该表单向网站管理员发送电子

我构建了一个简单的联系人表单,在我的测试“mvc1application”中,它在本地非常有效。当我把准确的代码转移到我的一个网站上,在那里我会得到这个表格,我会遇到问题,真的很难办

场景是当用户访问站点时,它会检查站点是否已通过身份验证(通过SSO系统)。如果是这样,则设置cookie并登录。如果您尝试在未登录的情况下访问该站点,则当前会被重定向到“AccessDenied.cshtml”视图。那没问题

此后,我用一个表单(简单的联系表单)取代了这个视图,用户可以填写该表单向网站管理员发送电子邮件。我得到了正确显示的表单,在POST上它得到了一个200,通过Chrome开发工具我看到表单内容被发布。没有发生的是,我认为我的EmailViewModel.cs没有被调用并处理表单数据。因此,从未发送过电子邮件

我真的很困惑,下面是一些示例代码:

(我的AccountController有些零件被剪掉了…)

还有我的EmailViewModel.cs

namespace MVC.Models
{
    public class EmailViewModel
    {
        [Required]
        public string Name { get; set; }

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

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

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

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

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

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

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


    }
}
最后是我的Requestform.cshtml视图

@model CorpNewsMgr.Models.EmailViewModel
@{
    ViewBag.Title = "Request Access";
    Layout = "~/Views/Shared/_WideLayoutDenied.cshtml";
}
<p>
    Currently you do not have the required credentials to access the site.
    <br />
    To request more information, or to request permission to access this system, please
    contact:
</p>
<br />
<br />
<div>
    <h3>
        Please fill this form out to request access to the tool.</h3>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Request Access</legend>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.Name)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.Name)
                @Html.ValidationMessageFor(Model => Model.Name)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.EmailAddress)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.EmailAddress)
                @Html.ValidationMessageFor(Model => Model.EmailAddress)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalTeamName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalTeamName)
                @Html.ValidationMessageFor(Model => Model.InternalTeamName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalTeamLeadName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalTeamLeadName)
                @Html.ValidationMessageFor(Model => Model.InternalTeamLeadName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalTeamLeadEmail)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalTeamLeadEmail)
                @Html.ValidationMessageFor(Model => Model.InternalTeamLeadEmail)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalDistEmail)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalDistEmail)
                @Html.ValidationMessageFor(Model => Model.InternalDistEmail)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.AverageNumCommsWeekly)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.AverageNumCommsWeekly)
                @Html.ValidationMessageFor(Model => Model.AverageNumCommsWeekly)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.AverageNumPeopleSentWeekly)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.AverageNumPeopleSentWeekly)
                @Html.ValidationMessageFor(Model => Model.AverageNumPeopleSentWeekly)
            </div>
            <p>
                <input type="submit" value="Send" />
            </p>
        </fieldset>

    }
</div>
<br />
<p>
    Thank you,<br />

</p>
@model CorpNewsMgr.Models.EmailViewModel
@{
ViewBag.Title=“请求访问”;
Layout=“~/Views/Shared/\u WideLayoutDenied.cshtml”;
}

当前您没有访问该站点所需的凭据。

要请求更多信息,或请求访问此系统的权限,请 联系人:



请填写此表格以请求访问该工具。 @使用(Html.BeginForm()) { @Html.ValidationSummary(true) 请求访问 @LabelFor(Model=>Model.Name) @Html.TextBoxFor(Model=>Model.Name) @Html.ValidationMessageFor(Model=>Model.Name) @LabelFor(Model=>Model.EmailAddress) @Html.TextBoxFor(Model=>Model.EmailAddress) @Html.ValidationMessageFor(Model=>Model.EmailAddress) @LabelFor(Model=>Model.InternalTeamName) @Html.TextBoxFor(Model=>Model.InternalTeamName) @Html.ValidationMessageFor(Model=>Model.InternalTeamName) @LabelFor(Model=>Model.InternalTeamLeadName) @Html.TextBoxFor(Model=>Model.InternalTeamLeadName) @Html.ValidationMessageFor(Model=>Model.InternalTeamLeadName) @LabelFor(Model=>Model.InternalTeamLeadEmail) @Html.TextBoxFor(Model=>Model.InternalTeamLeadEmail) @Html.ValidationMessageFor(Model=>Model.InternalTeamLeadEmail) @LabelFor(Model=>Model.InternalDistEmail) @Html.TextBoxFor(Model=>Model.InternalDistEmail) @Html.ValidationMessageFor(Model=>Model.InternalDistEmail) @LabelFor(Model=>Model.AverageNumCommsWeekly) @Html.TextBoxFor(Model=>Model.AverageNumCommsWeekly) @Html.ValidationMessageFor(Model=>Model.AverageNumCommsWeekly) @LabelFor(Model=>Model.averagenumpeoplementweekly) @Html.TextBoxFor(Model=>Model.averagenumpeoplementweekly) @Html.ValidationMessageFor(Model=>Model.averagenumpeoplementweekly)

}
谢谢你,

因此,当页面被加载时,表单加载良好(耶)。我可以很好地填写并验证它(耶)。在提交时,我得到了200分,帖子看起来不错(我可以看到帖子中的字段),但什么也没发生。页面以空表单的形式重新加载,我在我的收件文件夹中看不到任何电子邮件或其他内容

同样,在我的本地开发测试站点上,我可以让它工作并通过ok。在这里我不能。我认为问题在于AccountController的顶部以及它是如何拉动视图的,但我很难(也很累)尝试弄清楚这一点

哦,这是我第一次从标准WebForms进入MVC表单构建领域

有什么想法吗

谢谢!

我本来想发表评论的,但时间太长了

您需要发布
Requestform.cshtml
发布到的操作,这里也不需要您的帐户控制器代码。我猜您的托管环境是共享托管,有一些东西阻止了您的电子邮件代码的运行

您可能要做的第一件事是,如果您的电子邮件代码位于
try catch
中,请临时注释我们的
try catch
,这样它将抛出并出错。这至少有助于让你了解正在发生的事情

下一步要做的是尝试为mvc3添加
Elmah
,您可以通过Visual Studio中的Nuget添加它。确保使用Elmah for MVC3 one,因为它将为您设置所有内容。这将允许您查看是否有任何错误被抛出


否则,在看不到您的实际电子邮件代码的情况下,我无法提供更多帮助。

您需要有一个操作结果来处理表单的发布。终于让它工作了。我最后不得不将
[AllowAnonymous]
添加到我的控制器操作中,因为它一直试图重定向和循环系统。不过,谢谢你的想法。现在,托管在我的内部开发框中。专用站点/主机。我把控制器代码放在那里,正如我在下面的一个例子中看到的(就像我说我是MVC的新手一样)。我将尝试try/catch语句,看看是否能找到它的位置
@model CorpNewsMgr.Models.EmailViewModel
@{
    ViewBag.Title = "Request Access";
    Layout = "~/Views/Shared/_WideLayoutDenied.cshtml";
}
<p>
    Currently you do not have the required credentials to access the site.
    <br />
    To request more information, or to request permission to access this system, please
    contact:
</p>
<br />
<br />
<div>
    <h3>
        Please fill this form out to request access to the tool.</h3>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Request Access</legend>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.Name)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.Name)
                @Html.ValidationMessageFor(Model => Model.Name)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.EmailAddress)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.EmailAddress)
                @Html.ValidationMessageFor(Model => Model.EmailAddress)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalTeamName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalTeamName)
                @Html.ValidationMessageFor(Model => Model.InternalTeamName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalTeamLeadName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalTeamLeadName)
                @Html.ValidationMessageFor(Model => Model.InternalTeamLeadName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalTeamLeadEmail)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalTeamLeadEmail)
                @Html.ValidationMessageFor(Model => Model.InternalTeamLeadEmail)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalDistEmail)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalDistEmail)
                @Html.ValidationMessageFor(Model => Model.InternalDistEmail)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.AverageNumCommsWeekly)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.AverageNumCommsWeekly)
                @Html.ValidationMessageFor(Model => Model.AverageNumCommsWeekly)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.AverageNumPeopleSentWeekly)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.AverageNumPeopleSentWeekly)
                @Html.ValidationMessageFor(Model => Model.AverageNumPeopleSentWeekly)
            </div>
            <p>
                <input type="submit" value="Send" />
            </p>
        </fieldset>

    }
</div>
<br />
<p>
    Thank you,<br />

</p>