我很新 KnockoutJS
我想用JSON GET retreieve部分视图和所有绑定验证 .
我的型号:
public class MyFormModel
{
[Required]
[Range(typeof(Decimal), "1", "9999")]
[Display(Name = Translations.WorkReport.PRICE_PER_UNIT)]
//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#,##}")]
public decimal Price { get; set; }
[Required]
[Range(typeof(Decimal), "1", "9999")]
[Display(Name = Translations.WorkReport.QUANTITY)]
//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#,##}")]
public decimal Quantity { get; set; }
...
我的控制器:
[HttpGet]
public virtual PartialViewResult GetDataByGroupId(int groupId)
{
...
var view = PartialView("WorkReportResult", createWorkReportsListFormModel);
return view
}
我的部分观点:
@for (int i = 0; i < Model.WorksReportsFormModel.WorkReportViewModelList.Count; i++)
{
<tr>
<td class="center">
@Html.ValidationMessageFor(modelItem => Model.WorksReportsFormModel.WorkReportViewModelList[i].WorkReportFormModel.Price)
@Html.TextBoxFor(modelItem => Model.WorksReportsFormModel.WorkReportViewModelList[i].WorkReportFormModel.Price,
new
{
@Value = Model.WorksReportsFormModel.WorkReportViewModelList[i].WorkReportFormModel.Price != 0 ? Model.WorksReportsFormModel.WorkReportViewModelList[i].WorkReportFormModel.Price.ToString() : "",
id = @Html.RenderNumbers("Price", i),
@class = "input-small focused"
})
</td>
<td class="center">
@Html.ValidationMessageFor(modelItem => Model.WorksReportsFormModel.WorkReportViewModelList[i].WorkReportFormModel.Quantity)
@Html.TextBoxFor(modelItem => Model.WorksReportsFormModel.WorkReportViewModelList[i].WorkReportFormModel.Quantity,
new
{
@Value = Model.WorksReportsFormModel.WorkReportViewModelList[i].WorkReportFormModel.Quantity != 0 ? Model.WorksReportsFormModel.WorkReportViewModelList[i].WorkReportFormModel.Quantity.ToString() : "",
id = @Html.RenderNumbers("Quantity", i),
@class = "input-small focused"
})
</td>
我的正常观点:
<table class="table table-striped table-bordered bootstrap-datatable datatable dataTable grid-table" id="students">
<thead>
<tr>
<th class="grid-header"><div class="grid-header-title">Price</div></th>
<th class="grid-header"><div class="grid-header-title">Wuantity</div></th>
</tr>
</thead>
<tbody id="content">
</tbody>
</table>
和我的JQUERY从这个视图调用来检索局部视图:
$.ajax({
type: 'GET',
timeout: 30000,
url: '' + baseurl + '/CustomModel/ManageWorkReport/GetDataByGroupId',
data: { groupId: value},
cache: false,
success: function (data) {
if (data) {
$("#content").html(data);
...
但是不行,这里没有绑定 . 如何包含 KnockoutJS
?
我发现了这个但是没有用! https://stevemgentile.wordpress.com/2012/10/07/knockoutjs-asp-net-mvc-partial-view-loading/
1 回答
我正在使用这样的东西:
在ViewModel中我做:
在我的View.cshtml中:
和模型(在c#中):
我希望这对您的问题有所帮助 .