action参数返回null

时间:2017-10-25 11:34:06

标签: jquery asp.net-mvc

这里已经有一些类似的帖子,并尝试了所有解决方案,但仍然无效......我无法在控制器内获取值,它始终为null。下面是代码。我错过了什么吗?

客户端javascript

 var arr = [];
        $('.myForm').each(function () {
            var ids = {
               n:$('#name').val(),
                c: $('#student_class').val(),
                g: $('#gender').val(),
                p: $('#phone').val()
            }
            arr.push(ids);

        });
        $.ajax({
            url: "/Home/InsertStudent/",
            type: "POST",
            cache: false,
            contentType: 'application/json',
            datatype:"html",
            data: JSON.stringify(arr),
            success: function (result) {
                alert("success");
               // $(" ").html(result)
            },
            error: function () {
                alert("error");
            }
        });

MVC控制器

 public PartialViewResult InsertStudent(StudentData model)
    {
        return PartialView();
    }

模型类

 public class StudentData
{
    public int id { get; set; }
    public string name { get; set; }
    public string student_class{ get; set; }
    public bool gender { get; set; }
    public int phone { get; set; }
}

我的观点

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()


<div class="container">
    <div class="row">
        <div class="col-md-2">
            <div><input type="button" value="Admin" id="admin" class="btn btn-default" onclick="Admin()" /></div>
        </div>
        <div class="col-md-2">
            <div><input type="button" value="Student" id="student" class="btn btn-default" onclick="Student()" /></div>
        </div>
        <div class="col-md-8">
            <div id="partial">
                @*@{ Html.Partial("InsertStudent");}*@
            </div>
        </div>
    </div>
</div>
                    }

2 个答案:

答案 0 :(得分:0)

将按钮的类型属性更改为按钮。 在Javascript中进行以下更改:

function save(){

     // var arr = []; Comment out the array
     var ids; // Declare ids out side the eachloop

                   var ids = {
      name :$('#name').val(),
      student_class: $('#student_class').val(),
      gender : $('#gender').val(),
      phone : $('#phone').val()
}
                  //  arr.push(ids); //Comment out this 


                $.ajax({
                    url: "/Home/InsertStudent/",
                    type: "POST",
                    cache: false,
                    contentType: 'application/json',
                    datatype:"html",
                    data: JSON.stringify(ids), // send ids instead of array
                    success: function (result) {
                        alert("success");
                       // $(" ").html(result)
                    },
                    error: function () {
                        alert("error");
                    }
                });

}

答案 1 :(得分:0)

@model Demo.Models.StudentData
@{
ViewBag.Title = "InsertStudent";
}

<div class="jumbotron text-center">
<h3>Student Portal</h3>
<p>Please enter your information here....</p>
</div>
<div class="col-md-4">
<div class="container">
    <div class="myForm">
    <div class="form-group row">
    @Html.HiddenFor(m => m.id)
@Html.Label("Name")
@Html.TextBoxFor(m => m.name)
    </div>

    <div class="form-group row">
        @Html.Label("Class")
        @Html.DropDownList("student_class", new List<SelectListItem>
   {new SelectListItem {Text="I",Value="1" },
   new SelectListItem {Text="II",Value="2" },
   new SelectListItem {Text="III",Value="3" },
       new SelectListItem {Text="IV",Value="4" },
       new SelectListItem {Text="V",Value="5" }
   }, "select")
    </div>
    <div class="form-group row">
        @Html.Label("Gender")
        @Html.RadioButtonFor(m => m.gender, "M", true) Male
        @Html.RadioButtonFor(m => m.gender, "F", false) Female
    </div>
    <div class="form-group row">
        @Html.Label("Phone")
        @Html.TextBoxFor(m => m.phone)
    </div>
                <input type="submit" value="Save" onclick="Save()"/>
            </div>