以下是我的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()方法中,限定值具有空值。请帮忙,为什么会这样。
答案 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){}