AJAX Post to ASP.NET核心MVC 6 Controller操作方法和参数为null

时间:2017-03-01 04:11:48

标签: jquery json asp.net-core-mvc asp.net-core-1.0

但是,在控制器中,参数始终为空/默认值。请参阅下面的代码,如果我犯了一个愚蠢的错误,请告诉我。

Plz see image

这是我的控制器的操作方法:

[HttpPost]
public JsonResult AjaxMethodTemp([FromBody]UserAccount user)
{
    //string Name = name + DateTime.Now.ToString();

    //return Json(Name);
    try
    {
        //if (ModelState.IsValid)
        //{
            _context.userAccount.Add(user);
            _context.SaveChanges();
            ModelState.Clear();

            ViewBag.Massege = user.FirstName + "_" + user.LastName + " is Successfully Registered";
        //}
    }
    catch (DbUpdateException /* ex */)
    {
        //Log the error (uncomment ex variable name and write a log.
        ModelState.AddModelError("", "Unable to save changes. " +
            "Try again, and if the problem persists " +
            "see your system administrator.");
    }
    return Json(user.FirstName + "_" + user.LastName + " is Successfully Registered");

}

这是我的AJAX帖子:

function SaveCity() {
    debugger
    var $form = $('#contactForm');
    $form.removeData('validator');
    $form.removeData('unobtrusiveValidation');
    $.validator.unobtrusive.parse($form);
    $form.validate();
    if ($form.valid()) {

        $.ajax({
            type: "POST",
            url: "/Home/AjaxMethodTemp",
            //url:"/api/users",
            contentType: "application/json; charset=utf-8",
            data:
                {
                    Password: "abc",
                    UserName: "abc",
                    Email: "abc@gmail.com",
                    FirstName: "abc",
                    LastName: "abc"
                },
            //data: JSON.stringify({
            //    FirstName: $('#FirstName').val(),
            //    LastName: $("#LastName").val(),
            //    Email: $("#Email").val(),
            //    UserName: $("#UserName").val(),
            //    Password: $("#Password").val()
            //}),
            success: function (data) {
            }

        });

    }
}

以下是我的表单数据:

<form id="contactForm">
    @* asp-controller="Home" asp-action="AjaxMethod method="post""*@
    @if (ViewBag.Massege != null)
    {
        <div class="form-control">
            <div class="col-md-10">@ViewBag.Massege</div>
        </div>
    }

    <div class="form-group">
        <label asp-for="FirstName" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="FirstName" id="FirstName" class="form-control" />
            <span asp-validation-for="FirstName" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="LastName" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="LastName" id="LastName" class="form-control" />
            <span asp-validation-for="LastName" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="Email" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="Email" id="Email" class="form-control" />
            <span asp-validation-for="Email" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="UserName" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="UserName" id="UserName" class="form-control" />
            <span asp-validation-for="UserName" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="Password" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="Password" id="Password" class="form-control" />
            <span asp-validation-for="Password" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="ConfirmPassword" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="ConfirmPassword" class="form-control" />
            <span asp-validation-for="ConfirmPassword" class="text-danger"></span>
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            @*<button type="submit" class="btn btn-default" id="btn1">Register</button>*@

            @*<input type="button" id="btnGet" value="Get Current Time" />*@
            <input type="submit" name="Sumbit" onclick="SaveCity(); return false;" />
            @*<a class="delete-link" h>Delete</a>*@
        </div>
    </div>
</form>

以下是模型:

public class UserAccount
{
    [Key]
    public int UserID { get; set; }

    [Required(ErrorMessage ="First Name Required")]
    public string FirstName { get; set; }

    [Required(ErrorMessage ="Last Name Required")]
    public string LastName { get; set; }

    [Required(ErrorMessage ="Email Required")]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [Required(ErrorMessage ="User Name Required")]
    public string UserName { get; set; }

    [Required(ErrorMessage ="Password Required")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Compare("Password",ErrorMessage ="Password Not Match")]
    [DataType(DataType.Password)]
    public string ConfirmPassword { get; set; }
}

任何人都有想法,而不是帮助我...

1 个答案:

答案 0 :(得分:0)

试试这个,

var UserAccount{
    Password: "abc",
    UserName: "abc",
    Email: "abc@gmail.com",
    FirstName: "abc",
    LastName: "abc"
};

 $.ajax({
      type: "POST",
      url: "/Home/AjaxMethodTemp",                      
      data: { user: UserAccount},           
      success: function (data) {
        alert("SUCCESS");
      }    
  });