在运行时使用来自indexeddb的数据填充cshtml

问题描述 投票:0回答:1

我在浏览器的indexedDB中保存了一个viewmodel,我已缓存了html页面。在下面的示例(仅视图的一部分)中,保存在indexeddb中的模型是InspectionUpsertViewModel:

@using Common.OM
@using ACA.OM.Tag_M
@model ACA.WebERP.Models.Inspection_M.InspectionUpsertViewModel
<div class="content">
    <div class="block block-rounded block-bordered">

        <ul class="nav nav-tabs nav-tabs-alt">
            <li class="nav-item">
                <a id="step1" class="nav-link active" data-toggle="tab" href="#tab-step-1">@ACA.Translations.Measurement.Step1</a>
            </li>
            <li class="nav-item">
                <a class="nav-link disabled" data-toggle="tab" href="#tab-step-2">@ACA.Translations.Measurement.Step2</a>
            </li>
            <li class="nav-item">
                <a id="step3" class="nav-link disabled" data-toggle="tab" href="#tab-step-3">@ACA.Translations.Measurement.Step3</a>
            </li>
        </ul>

        <div class="content">
            <div class="alert alert-danger" role="alert" id="subTypeChangeAlert" style="display:none;">
                @ACA.Translations.Measurement.AlertSubTypeChange
            </div>
            <h4>@ACA.Translations.Inspection.InspectionNr @Model.Inspection.Nr</h4>
        </div>

        @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "step1-form" }))
        {
            @Html.HiddenFor(model => model.Inspection.Id)
            @Html.AntiForgeryToken()

            <div class="tab-pane active" id="tab-step-1" role="tabpanel">
                <div class="block-content">
                    <div class="form-group row">
                        @Html.LabelFor(model => model.Inspection.Article, new { @class = "col-form-label col-md-2" })
                        @Html.HiddenFor(model => model.Inspection.ArticleId)
                        @Html.HiddenFor(model => model.Inspection.Article.AddressId)
                        @Html.HiddenFor(model => model.Inspection.Article.ArticlePropertiesVersion.Id)
                        @Html.HiddenFor(model => model.IsCreatedOnline)
                        @Html.HiddenFor(model => model.QuestionaireId);
                        <div class="col-md-10">
                            <div class="form-group row" id="article" style="@(Model.Inspection.Article != null ? "" : "display: none")">
                                <div class="col-md-10">
                                    <div>
                                        <span class="help-block" id="internNr">@(Model.Inspection.Article != null && !Model.Inspection.Article.InternNr.IsNullOrWhiteSpace() ? $"{ACA.Translations.Article.InternNr}: {Model.Inspection.Article.InternNr}" : "")</span>
                                    </div>
                                    <div>
                                        <span class="help-block" id="serialNr">@(Model.Inspection.Article != null && !Model.Inspection.Article.SerialNr.IsNullOrWhiteSpace() ? $"{ACA.Translations.Article.SerialNr}: {Model.Inspection.Article.SerialNr}" : "")</span>
                                    </div>
                                    <div>
                                        <span class="help-block" id="type">@(Model.Inspection.Article != null ? $"{ACA.Translations.Article.Type}: {Model.Inspection.Article.Type?.Name} - {Model.Inspection.Article.SubType?.Name}" : "")</span>
                                    </div>
                                </div>
                            </div>
                            <div>
                                <button type="button" class="btn btn-warning editArticleButton" data-toggle="modal" data-target="#editArticleModal"><i class="fa fa-pencil-alt"></i></button>
                            </div>
                        </div>
                    </div>

现在,我需要在运行时填充数据,因此从indexedDB获取我的对象并在视图中填充对象值。有人对如何执行此操作有想法吗?

提前感谢。

javascript jquery .net view indexeddb
1个回答
0
投票

带有实际模型值的Razor语法的唯一方法是,在加载视图之前,先在控制器操作中加载它们。因此,为了正确加载值,您可以执行以下操作:

return View(Model)

其中Model是包含您的数据的对象。

为了在视图已加载时填充这些字段,是通过Javascript向控制器中的其他Action发出ajax请求,该请求以以下方式结束:

return JSON(Model)

并将返回模型及其数据,然后使用jQuery或任何您想要的东西将它们加载到表单中。

*编辑*

要比jquery更快地完成此任务,您需要使用实现数据绑定的js库。这样的示例是根据最适合您的需求而开发的基因敲除js和铆钉js]

© www.soinside.com 2019 - 2024. All rights reserved.