【问题标题】:Fill cshtml at runtime with data from indexeddb在运行时使用 indexeddb 中的数据填充 cshtml
【发布时间】:2019-10-17 10:07:18
【问题描述】:

我有一个视图模型保存在浏览器的 indexedDB 中,我缓存了 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】:

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

    return View(Model)

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

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

    return JSON(Model)

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

    * 编辑 *

    要比 jquery 更快地完成这项任务,您需要使用实现数据绑定的 js 库。此类示例为 knockout.jsrivets.js,具体取决于最适合您的需求

    【讨论】:

    • 我已经在 indexeddb 中保存了return JSON(Model) 部分,但是我如何使用 jquery 来填写呢?我想使用document.getelementById() 但这只是很多工作.. 我只是想知道如何填写这些数据.. 页面需要脱机工作,所以我不能使用任何控制器/ajax 请求..
    • @JelledeKeukeleire 我编辑了我的答案。请再次检查并批准,因为这是您要查找的内容。
    猜你喜欢
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    相关资源
    最近更新 更多