在我的personDetails.cshmtl文件中,我有以下表格:
<form id="userUpdateForm" method="post">
<fieldset>
<legend>User Details</legend>
<input type="checkbox" name="authorisedCheckbox" value="Authorised" id="authCheck" />Authorised<br />
<input type="checkbox" name="enabledCheckbox" value="Enabled" id="eCheck" />Enabled<br />
</fieldset>
<fieldset>
<input type="hidden" name="personId" id="idCheck" value='@ViewData["personId"]'>
</fieldset>
<input type="submit" value="Save Changes" name="Save Changes">
<button type="button">Cancel</button>
</form>
然后我在页面的下方继续使用Javascript,如下所示:
$('#userUpdateForm').submit(function (e) {
var personDetails = {
Enabled: $('#eCheck').val(),
Authorised: $('#authCheck').val(),
Id: $('#idCheck').val()
};
$.ajax({
type: "POST",
url: '<%= Url.Action("submitForm", "Home") %>',
data: JSON(personDetails),
//contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
'@Url.Action("Index", "Home")';
},
error: function (result) {
alert("A problem occured when submitting the form.");
}
});
return false;
});
Javascript意味着在点击“提交”按钮时触发。首先,'HomeController'中的方法'submitForm'被触发,然后在'HomeController'中重定向回'Index',它只输出一个视图。
'submitForm'目前类似于以下内容:
[HttpPost]
public ActionResult submitForm(JsonResult jsonForm)
{
System.Diagnostics.Debug.WriteLine("made it here");
System.Diagnostics.Debug.WriteLine(jsonForm);
return View();
}
所以我的问题是:我目前的表格中缺少什么才能使AJAX事件发生?我很确定它当前没有被触发,因为控制台没有任何警报和输出,它应该击中它的方法。
我宁愿不要过多地更改表单本身,因为我目前有另外一些Javascript设置复选框以在适当的时候检查onload。
编辑:我用Stephen Muecke的建议修正了这个方法。我在这里要做的是:点击按钮 - &gt;将详细信息提交到我的submitForm方法 - &gt; submitForm方法需要详细信息并在数据库中执行一些操作 - &gt;完成后返回成功 - &gt;成功时,从personDetails.cshtml重定向到index.cshtml。答案 0 :(得分:2)
您需要创建一个其他类,该类应包含您要发布到submitForm操作方法的所有属性。
目前您使用的JsonResult类不包含您尝试发布的属性,因此ModelBinder不会绑定数据。参考Model Binder ..好文章......喜欢它....
ld: warning: arm64 function not 4-byte aligned: ltmp0 from /Users/user/unityWorkspace/GoodTest2/NOgpgs/Libraries/libiPhone-lib.a(unwind_test_arm64.o)
ld: warning: arm64 function not 4-byte aligned: _unwind_tester from /Users/user/unityWorkspace/GoodTest2/NOgpgs/Libraries/libiPhone-lib.a(unwind_test_arm64.o)
Undefined symbols for architecture arm64:
"_kSecValueData", referenced from:
-[GTMOAuth2Keychain setPassword:forService:accessibility:account:error:] in GoogleSignInDependencies(GTMOAuth2ViewControllerTouch_5e9c07ce1f20710c59c1499209a8b99a.o)
"_SecTrustEvaluate", referenced from:
___70+[GTMSessionFetcher evaluateServerTrust:forRequest:completionHandler:]_block_invoke in GoogleSignInDependencies(GTMSessionFetcher_28beddf8a47a0abe7f4eaa9a5c9cb75b.o)
"_kSecAttrAccessible", referenced from:
-[GTMOAuth2Keychain setPassword:forService:accessibility:account:error:] in GoogleSignInDependencies(GTMOAuth2ViewControllerTouch_5e9c07ce1f20710c59c1499209a8b99a.o)
"_SecItemAdd", referenced from:
-[GTMOAuth2Keychain setPassword:forService:accessibility:account:error:] in GoogleSignInDependencies(GTMOAuth2ViewControllerTouch_5e9c07ce1f20710c59c1499209a8b99a.o)
"_SecItemDelete", referenced from:
-[GTMOAuth2Keychain removePasswordForService:account:error:] in GoogleSignInDependencies(GTMOAuth2ViewControllerTouch_5e9c07ce1f20710c59c1499209a8b99a.o)
"_kSecReturnData", referenced from:
-[GTMOAuth2Keychain passwordForService:account:error:] in GoogleSignInDependencies(GTMOAuth2ViewControllerTouch_5e9c07ce1f20710c59c1499209a8b99a.o)
"_kSecClass", referenced from:
+[GTMOAuth2Keychain keychainQueryForService:account:] in GoogleSignInDependencies(GTMOAuth2ViewControllerTouch_5e9c07ce1f20710c59c1499209a8b99a.o)
"_SecItemCopyMatching", referenced from:
-[GTMOAuth2Keychain passwordForService:account:error:] in GoogleSignInDependencies(GTMOAuth2ViewControllerTouch_5e9c07ce1f20710c59c1499209a8b99a.o)
"_kSecAttrGeneric", referenced from:
+[GTMOAuth2Keychain keychainQueryForService:account:] in GoogleSignInDependencies(GTMOAuth2ViewControllerTouch_5e9c07ce1f20710c59c1499209a8b99a.o)
"_kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly", referenced from:
+[GTMOAuth2ViewControllerTouch saveParamsToKeychainForName:accessibility:authentication:error:] in GoogleSignInDependencies(GTMOAuth2ViewControllerTouch_5e9c07ce1f20710c59c1499209a8b99a.o)
"_kSecMatchLimitOne", referenced from:
-[GTMOAuth2Keychain passwordForService:account:error:] in GoogleSignInDependencies(GTMOAuth2ViewControllerTouch_5e9c07ce1f20710c59c1499209a8b99a.o)
"_kSecAttrService", referenced from:
+[GTMOAuth2Keychain keychainQueryForService:account:] in GoogleSignInDependencies(GTMOAuth2ViewControllerTouch_5e9c07ce1f20710c59c1499209a8b99a.o)
"_kSecClassGenericPassword", referenced from:
+[GTMOAuth2Keychain keychainQueryForService:account:] in GoogleSignInDependencies(GTMOAuth2ViewControllerTouch_5e9c07ce1f20710c59c1499209a8b99a.o)
"_kSecRandomDefault", referenced from:
-[GIDSignIn randomString] in GoogleSignIn(GIDSignIn.o)
"_kSecAttrAccount", referenced from:
+[GTMOAuth2Keychain keychainQueryForService:account:] in GoogleSignInDependencies(GTMOAuth2ViewControllerTouch_5e9c07ce1f20710c59c1499209a8b99a.o)
"_kSecMatchLimit", referenced from:
-[GTMOAuth2Keychain passwordForService:account:error:] in GoogleSignInDependencies(GTMOAuth2ViewControllerTouch_5e9c07ce1f20710c59c1499209a8b99a.o)
"_SecRandomCopyBytes", referenced from:
-[GIDSignIn randomString] in GoogleSignIn(GIDSignIn.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
答案 1 :(得分:1)
示例:
型号:
public class MyModel{
public string Id {get;set;}
public string Authorised {get;set;}
public string Enabled {get;set;}
}
动作:
[HttpPost]
public ActionResult submitForm(MyModel model)
{
System.Diagnostics.Debug.WriteLine("made it here");
System.Diagnostics.Debug.WriteLine(jsonForm);
return View();
}
脚本:
$('#userUpdateForm').submit(function (e) {
var personDetails = {
Id: $('#idCheck').val(),
Authorised: $('#authCheck').val(),
Enabled: $('#eCheck').val()
};
$.ajax({
type: "POST",
url: '@Url.Action("submitForm", "Home")',
contentType: "application/json",
data: JSON.stringify({ model: personDetails })
success: function (result) {
window.location.href = "/home/index/";
},
error: function (result) {
alert("A problem occured when submitting the form.");
}
});
return false;
});