使用jquery / ajax将数据发送到mvc控制器

时间:2017-04-01 16:19:22

标签: jquery .net json ajax asp.net-mvc

在我的MVC应用程序中,这是我的模型:

public class Student
    {
        public string StudentId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
    }

我的.cshtml就像:

@using (Ajax.BeginForm(null, "TestAjaxBeginForm", null , null))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Student</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.StudentId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.StudentId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.StudentId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" onclick="GetData()"/>
            </div>
        </div>
    </div>
}

我正在使用jquery从.cshtml文件中在控制器中发送数据。

<script>
    function GetData()
    {
        debugger;
         var Student = [
        {
            'StudentId': $('#StudentId').val(),
            'Name': $('#Name').val(),
            'Address': $('#Address').val()
        }
        ];
        Student = JSON.stringify({ 'Student': Student });

        $.ajax({
            type: "POST",
            url: "/Student/GetData/",
            dataType: "json",
            contentType: "application/json",
            data: Student,
            processData: true,
            success: function (data) {
                alert("Hi");
                //alert(data);
            },
            error: function (xhr, ajaxOptions, error) {
                alert(xhr.status);
                alert("Error: " + xhr.responseText);
                //alert(error);
            }
        });
    }

</script>

我的控制器是:

   [HttpPost]
    public ActionResult GetData(Student Student)
    {
        var StudentId = Student.StudentId.ToString();
        var StudentName = Student.Name.ToString();
        var Address = Student.Address.ToString();
        return Json(JsonRequestBehavior.AllowGet);
    }

我在jscript函数中获取表单数据,但在StudentId的控制器(GetData)值内,StudentName和地址显示为null。 感谢您的帮助。

由于 帕塔

1 个答案:

答案 0 :(得分:0)

将其更改为以下内容应该有效:

    var Student = 
    {
        StudentId: $('[name="StudentId"').val(),
        Name: $('[name="Name"]').val(),
        Address: $('[name="Address"]').val()
    }

此外,无需JSON.stringify电话