来自JSON GET调用的Knockout和部分视图

时间:2016-01-17 14:11:51

标签: jquery asp.net asp.net-mvc knockout.js

我对KnockoutJS很新。我希望JSON GET使用验证检索部分视图和所有绑定。

我的模特:

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 个答案:

答案 0 :(得分:0)

我正在使用这样的东西:

var unmapped = ko.mapping.toJS(self.Model);
var unmappedJSON = ko.toJSON(unmapped);

$.ajax({
            url: ...,
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json',
            data: unmappedJSON,
            success: function (data, textStatus, jqXHR) {
                //var mapping = self.Model.IgnoreMapping();
                var mapping = []
                var viewModel = ko.mapping.fromJS(data, mapping, self.Model);

                // TODO:  Show errors from the server's validation
            }

            error: ...
});

在ViewModel中我做了:

    var vm = new ViewModel();

    var d = @(Html.Raw(Json.Encode(Model))); 
    self.Model = ko.mapping.fromJS(d);

    ko.applyBindings(vm);

在我的View.cshtml中:

        @Html.LabelFor(model => model.Description, "Description")
        @Html.TextAreaFor(model => model.Description, new { data_bind = "value: Model.Description" })
        @Html.ValidationMessageFor(model => model.Description)

模型(在c#中):

    [Required]
    public string Description { get; set; }

我希望这对您的问题有所帮助。