0

I have a partial view of saving email in newsletter mailing list that is used in Main partial view of the site. when click on its button, it should send form to services.js ,then send to action for saving in Database. But it saves "null" in it, when I set a break point on action, it doesn't go with it. what should i do?

Newsletter Model:

 public class NewsletterModel
    {
        [Required(ErrorMessage = "Please enter your email address!")]
        [Display(Name = "Email")]
        public string Email { get; set; }

        public Int16 LanguageId { get; set; }
    }

partial view code:

    @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>
}

This is the action code:

[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 });
        }
    }

this code is for 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);
                }
            }
        });
    })
});
mortazavi
  • 401
  • 9
  • 22
  • You can send email and language as page parameters. – sgud Jan 27 '13 at 20:08
  • 1
    http://stackoverflow.com/questions/8517071/send-json-data-via-post-ajax-and-receive-json-response-from-controller-mvc hope this helps – sgud Jan 27 '13 at 20:15
  • @sgud thanks for helping, I had seen this link before, but when you send it and I saw it again , by using Jason.stringify and changing a few parts of the code , solved my problem. Thanks a lot. But I want to know about your first comment, please tell me how can I use that way and send as page parameter? – mortazavi Jan 27 '13 at 22:12

0 Answers0