Model is Filled on PartialAsync Call but Null on Partial View C# Razor ASPCORE

0

I am creating a partial Model Async due to constraints of a library I am using. I call the partial and return a Model containing my progressModel data. The model's ProgressModel has data when it leaves the server and when it hits the partial I get an Object Reference Null Error.

  Error:
  System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=NGL.Dashboard.Razor.Views
  StackTrace:
  at 
  NGL.Dashboard.Razor.Pages.Pages__WorkflowProgressBarPartial.get_Model()

Injection of Partial:

@await Html.PartialAsync("/Pages/_WorkflowProgressBarPartial.cshtml", 
PartialModel.OnPostUseMyClassAsync(log, mapper, options, workflowInstanceId).Result)

Partial Page Model:

  public class _WorkflowProgressBarPartialModel : PageModel
  {
    [BindProperty]
    public ProgressModel progressModel { get; set; }


    // https://blog.stephencleary.com/2013/01/async-oop-2-constructors.html //
    public sealed class ProgressModel {
        public WorkflowInstanceController WIC;
        public IWorkflowInstanceStore workflowInstanceStore;
        public IWorkflowDefinitionStore workflowDefinitionStore;
        public ElsaDashboardOptions _options { get; }
        public INotifier notifier;
        private ILogger _log;
        private IMapper _mapper;
        [BindProperty]
        public string WorkFlowInstanceId { get; set; }
        [BindProperty]
        public WorkflowInstanceDetailsModel WorkflowDetailsModel { get; set; }
        
        private ProgressModel(ILogger log, IMapper mapper, IOptions<ElsaDashboardOptions> options, string workFlowInstanceId)
        {
            _log = log;
            _mapper = mapper;
            WorkFlowInstanceId = workFlowInstanceId;
            WorkflowDataAccess da = new WorkflowDataAccess(_log);
            ElsaContext _elsaContext = da.GetElsaContext();
            workflowInstanceStore = new EntityFrameworkCoreWorkflowInstanceStore(_elsaContext, _mapper);
            workflowDefinitionStore = new EntityFrameworkCoreWorkflowDefinitionStore(_elsaContext, _mapper);
            notifier = new Notifier();

            WIC = new WorkflowInstanceController(
            workflowInstanceStore,
            workflowDefinitionStore,
            options,
            notifier);
        }

        private async Task<ProgressModel> InitializeAsync(string workFlowInstanceId)
        {
            WorkflowDetailsModel = await WfDetailsModel(workFlowInstanceId);
            return this;
        }

        public static Task<ProgressModel> CreateAsync(ILogger log, IMapper mapper, IOptions<ElsaDashboardOptions> options, string workFlowInstanceId)
        {
            var ret = new ProgressModel(log, mapper, options, workFlowInstanceId);
            return  ret.InitializeAsync(workFlowInstanceId);
        }
        public async Task<WorkflowInstanceDetailsModel> WfDetailsModel(string workFlowInstanceId)
        {
            System.Threading.CancellationToken cancellationToken = default;
            WorkflowInstanceDetailsModel DetailsModel = (WorkflowInstanceDetailsModel)await WIC.DetailsModel(workFlowInstanceId, cancellationToken);
            WorkflowDetailsModel = DetailsModel;
            return DetailsModel;
        }
    }
    public async Task<_WorkflowProgressBarPartialModel> OnPostUseMyClassAsync(ILogger log, IMapper mapper, IOptions<ElsaDashboardOptions> options, string workFlowInstanceId)
    {
        _WorkflowProgressBarPartialModel model = new _WorkflowProgressBarPartialModel();
        ProgressModel instance = await ProgressModel.CreateAsync(log, mapper, options, workFlowInstanceId);
        model.progressModel = instance;
        
        return model;

    }

}

Partial:

    @Page
    @model NGL.Dashboard.Razor.Pages._WorkflowProgressBarPartialModel
    @using System.Globalization

    <div class="table-responsive">
    <table class="table align-items-center table-flush">
    <thead class="thead-light">
        <tr>
            <th scope="col">Timestamp</th>
            <th scope="col">Activity ID</th>
            <th scope="col">Message</th>
        </tr>
    </thead>
    <tbody>
        @if (Model != null)
        {
     foreach (var item in Model.progressModel)
            {
           var statusClass = item.Faulted ? "bg-warning" : "bg- success";
                var message = item.Faulted ? item.Message : "";

                <tr data-activity-id="@item.ActivityId">
                    <td>
                        <span class="badge badge-dot mr-4">
                            <i class=@statusClass></i>
                        </span>
               @item.Timestamp.ToString("g", CultureInfo.CurrentCulture)
                    </td>
                    <td>@item.ActivityId</td>
                    <td>@message</td>
                </tr>
            }
        }
    </tbody>
</table>
.
c#
asp.net-core
razor
model
razor-pages
asked on Stack Overflow Feb 11, 2021 by Ray Koren • edited Feb 11, 2021 by Ray Koren

1 Answer

0

I had to remove @Page, Now it works!

If you want to use partial view,you can refer to the official doc,partial view don't have @page in view.And then the xx.cshtml.cs will not be used. – Yiyi You

answered on Stack Overflow Feb 13, 2021 by Ray Koren

User contributions licensed under CC BY-SA 3.0