我刚刚开始使用 Knockout.js。我已经阅读了有关使用 mvc 中的 knockout.js 从数据库访问数据的各种教程,但没有任何效果。以下是我的表格:-
<form data-bind="submit: save" method="post" style="text-align: inherit;">
<table>
<tr>
<td style="text-align: right">
Name:
</td>
<td>
<input type="text" placeholder="Enter Your name" data-bind="value: name" required /><br />
</td>
</tr>
<tr>
<td style="text-align: right">
Emp#:
</td>
<td>
<input type="text" placeholder="Enter Your Code" required data-bind="value:code" />
</td>
</tr>
<tr>
<td style="text-align: right">
Date of Birth:
</td>
<td>
<input type="date" placeholder="Enter Your Date Of Birth" data-bind="value:date" />
</td>
</tr>
<tr>
<td style="text-align: right">
Age:
</td>
<td>
<input type="number" placeholder="AGE" min="18" max="60" data-bind="value:age" /><br />
</td>
</tr>
<tr>
<td style="text-align: right">
Contact Number:
</td>
<td>
<input type="text" placeholder="Enter Your Contact Number" data-bind="value:contact" />
</td>
</tr>
<tr>
<td style="text-align: right">
Email:
</td>
<td>
<input type="email" placeholder="Enter Your Email" data-bind="value:email" />
</td>
</tr>
<tr>
<td style="text-align: right">
Address:
</td>
<td>
<input type="text" placeholder="Enter Your Address" data-bind="value: address" />
</td>
</tr>
<tr>
<td style="text-align: right">
City:
</td>
<td>
<select>
<option value="city" data-bind="selectedOptions:optionselect">Noida</option>
<option value="city" data-bind="selectedOptions:optionselect">Mumbai</option>
</select>
</td>
</tr>
<tr>
<td style="text-align: right">
Marital Status:
</td>
<td>
<input type="radio" name="martialStatus" value="Married" data-bind="checked:radioselect" />Married
<input type="radio" name="martialStatus" value="UnMarried" data-bind="checked:radioselect" />UnMarried
</td>
</tr>
<tr>
<td style="text-align: right">
Any Employee Reffrence:
</td>
<td>
<input type="checkbox" name="referal" value="yes" data-bind="checked:checkboxchecked" />
</td>
</tr>
</table>
<div style="float: right; margin-right: 15px;">
<input type="submit" name="submit" value="Save" />
<button type="button" value="cancel" onclick="window.close(this);">
Cancel
</button>
</div>
</form>
我的javascript如下:-
<script type = "text/javascript">
var viewModel = {
name: ko.observable(""),
code: ko.observable(""),
date: ko.observable(""),
age: ko.observable(""),
contact: ko.observable(""),
email: ko.observable(""),
address: ko.observable(""),
optionselect: ko.observable(""),
radioselect: ko.observable(""),
checkboxchecked: ko.observable("")
var save = function(){
$.ajax("/Exercise/Exercise7", {
ko.toJSON(viewModel),
type: "post",
contentType: "application/json",
success: function(result) { alert(result) }
});
<script>
问题是:-
1)当我运行这个应用程序时,脚本没有从表单调用。2)如何将数据从脚本传输到我的控制器动作?
提前致谢!!