我正在使用Mvc4/bootstrap 3.2和knockout 3.2。問題我有一個表單有一個。文本框和按鈕,在按鈕上單擊我將文本框值傳遞給控制器。從表控制器綁定viewModel接收數據後。但是這兩個數據都沒有在表中更新,也沒有更新mvc模型。我在提交數據時檢查了一下。ASP.NET MVC敲除綁定不起作用
視圖模型:我已經在兩個方面
var SibviewModel=null;
$(function() {
var model = @Html.Raw(Json.Encode(Model));
SibviewModel = ko.mapping.fromJS(model);
ko.applyBindings(SibviewModel);
});
function GetMatchingSibling(_Id) {
var url = "/Home/GetStudentSiblingDetails/?Id=" + _Id;
$("#myModal").modal('show');
$.ajax({
url: url,
cache: false,
type: "GET",
success: function (data) {
try {
$(data.data).each(function (index, element) {
SibviewModel.SibviewModel.push(element);
});
ko.applyBindings(viewModel);
} catch (e) {
$(".alert-dismissable")
.alert('show')
.addClass("alert-danger")
.append("<h3>"+e+"</h3>");
}
finally {
$("#myModal").modal('hide');
}
},
error: function (reponse) {
alert("error : " + data.error);
$("#myModal").modal('hide');
}
});
}
試過ViewMoel然後我寫了MVC Razor視圖
<tbody data-bind="foreach: lookupCollection">
@foreach (var item in Model.LinkedSiblings)
{
string dobval = "";
if (Model.DOB.HasValue)
{
dobval = Model.DOB.Value.ToString("dd-MM-yyyy");
}
@Html.HiddenFor(x => item.PhoneNo, new { @Name = "LinkedSiblings[" + item.DynamicControlId + "].PhoneNo", @Value = item.PhoneNo, data_bind = "value: PhoneNo" })
@Html.HiddenFor(x => item.Email, new { @Name = "LinkedSiblings[" + item.DynamicControlId + "].Email", @Value = "[email protected]", data_bind = "value: Email" })
@Html.HiddenFor(x => dobval, new { @Name = "LinkedSiblings[" + item.DynamicControlId + "].DOB", @Value = dobval, data_bind = "value: DOB" })
@Html.HiddenFor(x => item.LastName, new { @Name = "LinkedSiblings[" + item.DynamicControlId + "].LastName", @Value = item.LastName, data_bind = "value: LastName" })
<tr>
<td data-bind="text: StudentId">@Html.TextBoxFor(x => item.StudentId, new { style = "max-width:100px;", @Name = "LinkedSiblings[" + item.DynamicControlId + "].StudentId", @Value = item.StudentId, data_bind = "value: StudentId" })
</td>
<td>@Html.TextBoxFor(x => item.AdmissionId, new { style = "max-width:100px;", @Name = "LinkedSiblings[" + item.DynamicControlId + "].AdmissionId", @Value = item.AdmissionId, data_bind = "value: AdmissionId" })
</td>
<td>@Html.TextBoxFor(x => item.FirstName, new { style = "max-width:100px;", @Name = "LinkedSiblings[" + item.DynamicControlId + "].FirstName", @Value = item.FirstName, data_bind = "value: FirstName" })
</td>
<td>@Html.TextBoxFor(x => item.ClassId, new { style = "max-width:100px;", @Name = "LinkedSiblings[" + item.DynamicControlId + "].ClassId", @Value = item.ClassId, data_bind = "value: ClassId" })
</td>
</tr>
}
@Html.EditorFor(x => Model.LinkedSiblings)
</tbody>
</table>
無論是觀與價值觀接收控制器上的方法不是價值觀提交時更新表格。
非常感謝您的迴應。實際上已經宣佈Viewmodel,但我沒有在這裏添加完整的代碼。我會提出建議的更改,但是當我提交表格時,會反映出我的意思(將列表傳遞給控制器操作)。 – 2014-11-11 11:45:17