0

我有一个在网站主要部分视图中使用的时事通讯邮件列表中保存电子邮件的部分视图。当点击它的按钮时,它应该将表单发送到 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);
                }
            }
        });
    })
});
4

0 回答 0