将<select>值发送到控制器</select>时出错

时间:2013-04-08 07:42:43

标签: ajax asp.net-mvc jquery

以下是我的HTML

<input type="text" name="name" id="name" />
<input type="text" name="age" id="age" />
<select  id="selectQualification">
            <option value="Select Qualification">Select Qualification</option>
                <option value="Bachelor">Bachelor</option>
                <option value="Graduate">Graduate</option>
                <option value="Master">Master</option>
                <option value="PostGraduate">Post Graduate</option>
                <option value="M.Phile">M.Phil</option>
                <option value="Ph.D">Ph.D</option>
                <option value="Other">Other</option>
        </select>
<input type="button" id="button" value="Submit"/>

这是JS

<script type="text/javascript">
        $(document).ready(function () {
            $("#button").click(function () {
                var personalInfo = new Object();
                personalInfo.name = $("#name").val();
                personalInfo.age=$("#age").val();
                personalInfo.qualification=$("#selectQualification").val();

                $.ajax({
                    type: "GET",
                    url: "@Url.Action("SubmitForm","Account")",
                    data: { name: $("#name").val(), fatherName: $("#fatherName").val(), selectQualification: $("#selectQualification").val() },
                    success: function () { alert("a"); }
            });
            });
        });

        </script>

以下是控制器方法

public void AcceptPersonalInfo(string name,string fatherName,string qualification) 
        {

        }

问题是在AcceptPersonalInfo()方法中,限定值具有空值。请帮忙,为什么会这样。

2 个答案:

答案 0 :(得分:2)

因为您发送的值为selectQualification,控制器所期望的值为qualification

所以要么改变

public void AcceptPersonalInfo(string name,string fatherName,string qualification) 
{}

public void AcceptPersonalInfo(string name,string fatherName,string selectQualification) 
{}

或更改

data: { name: $("#name").val(), fatherName: $("#fatherName").val(), selectQualification: $("#selectQualification").val() },

data: { name: $("#name").val(), fatherName: $("#fatherName").val(), qualification: $("#selectQualification").val() },

答案 1 :(得分:1)

您可以使用BindAttribute.Prefix财产。

例如:

public void AcceptPersonalInfo(string name,string fatherName,
                   [Bind(Prefix = "selectQualification")] string qualification){}