我对在网站的主要部分视图中使用的新闻邮件列表中保存电子邮件有部分看法。当点击它的按钮时,它应该将表单发送到services.js,然后发送到保存在数据库中的操作。但它在其中保存了“null”,当我在动作上设置断点时,它不会随之而来。我该怎么办?
时事通讯型号:
public class NewsletterModel
{
[Required(ErrorMessage = "Please enter your email address!")]
[Display(Name = "Email")]
public string Email { get; set; }
public Int16 LanguageId { get; set; }
}
部分查看代码:
@model Orinus.Models.NewsletterModel
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<link href="../../Content/Default/Site.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/Services.js" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<br />
<p>
<input type="button" value="Register" id="saveEmailNewsletter" class="ButtonStyle"/>
</p>
}
这是行动代码:
[HttpPost]
public ActionResult Index(NewsletterModel newsLetter)
{
if (!ModelState.IsValid)
{
return PartialView("_Newsletter", newsLetter);
}
SaveEmailForNewsletterSrv newsLetterSrv = new SaveEmailForNewsletterSrv();
newsLetterSrv.SaveEmailForNewsletter(newsLetter);
return Json(new { success = true });
}
}
此代码适用于Services.js:
/// <reference path="jquery-1.7.1.min.js" />
$(function () {
$("#saveEmailNewsletter").click(function () {
var email = $("#Email").val();
var language = 1;
newsLetter = { 'Email': email,'Language': language };
if (dat == null) {
alert("Please enter your email address!");
return;
}
$.ajax({
url: '/Newsletter/Index',
type: 'POST',
dataType: 'json',
data: newsLetter,
contentType: 'application/json; charset=utf-8',
success: function (result) {
if (result.success) {
// We have a JSON object in case of success
Alert ('Successfully registered...');
} else {
// We have the partial with errors in case of failure
// so all we have to do is update the DOM
$('#resultMessage').dialog(result);
}
}
});
})
});