Hi guys
I tried to post this question few minutes back, not appearing anywhere. My .Net Core 5.0 application has 4 different types of service tickets & my index page lists all pening service tickets. I can open a particular service ticket and view the details for it along with the comments against the same.
As the comments are saved against the service ticket id and the type of service ticket, I would like to use a partial view that I can call from all types of service ticket details view. I already know how to define a patial view and show it, however don't know how to bind a model based on the main view model.
For example, if I am viewing details for a service ticket for type "CA" and the service ticket id is "100", I want my partail view to show all comments associated with the id "100" and type "CA".
So far I created a view model that has both the service ticket details and comment objects & looks like that is irrelevant.
Please help ??


Rajesh Thampi131 reputationPosted Jun 14, 2023, 7:44 AM EDT
Hi Amit
I got it done! Actually I am passing the View Model, that consist models for both service ticket and the comments. I have to called the partial view passing the main view model
CommentsViewComponent
using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace ERPCustomerSupplier.Models { public class CommentsViewComponent : ViewComponent { public async Task InvokeAsync() { //var model = new CRViewModel(); //return await Task.FromResult((IViewComponentResult)View("Comments", model)); return await Task.FromResult((IViewComponentResult)View("Comments")); } } }Main View (Edited)
@using Microsoft.AspNetCore.Http @model ERPCustomerSupplier.Models.CRViewModel @{ ViewData["Title"] = "Edit Request"; Layout = "~/Views/Shared/_SiteMaster.cshtml"; } Edit Customer .... @(await Component.InvokeAsync("Comments",@Model)) @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }& Finally the comments partial view
@model ERPCustomerSupplier.Models.CRViewModel Comments @foreach (var item in Model.reqComments) { @Html.DisplayFor(modelItem => item.ID) @Html.DisplayFor(modelItem => item.USER_NAME) @Html.DisplayFor(modelItem => item.COMMENT_DATE) @Html.DisplayFor(modelItem => item.COMMENT_MSG) }passing model to the component call did the trick. (await Component.InvokeAsync("Comments",@Model))
Hope this helps few beginners like me out there!
Amit MohantyRank #112 · 20K reputationPosted Jun 14, 2023, 6:52 AM EDT
Partial view:
@model List Comments @if (Model != null && Model.Count > 0) { @foreach (var comment in Model) { @comment.Text } } else { No comments available. }In your main view for displaying service ticket details:
@Html.Partial("_Comments", Model.Comments)Rajesh Thampi131 reputationPosted Jun 14, 2023, 7:13 AM EDT
Hello @Amit
How do I get List of comments from the database before calling the partial view? As I said, I've my service ticket model already called on the main view and I want to pick the comments specific to the service ticket id.
Thanks for the answer!