Answered by:
partial view returns Value cannot be null

Question
-
User1051638994 posted
Hello,
I am trying to display a partial view with data from my db using a view model.
Here is my View model
public class VMTicketReply { public IEnumerable<Ticket> Tickets { get; set; } public IEnumerable<Replies> Replies { get; set; } public IEnumerable<User> Users { get; set; } public IEnumerable<Accepter> Accepters { get; set; } }
My razor view
@await Html.PartialAsync("_LoginPartial.cshtml", new VMTicketReply())
And my partial view
@model HelpDesk.Models.VMTicketReply @foreach (var item in Model.Tickets.Where(t=>t.TicketIsSeen = false)) { <a asp-action="Details" asp-route-id="@item.TicketID">@item.TicketContent></a> }
But always retuns ArgumentNullException: Value cannot be null. Any Idea? Thank you
Thursday, October 29, 2020 11:58 AM
Answers
-
User1312693872 posted
Hi,mspace
But always retuns ArgumentNullException: Value cannot be null. Any Idea? Thank youI haven't seen any data passing code in your shared code, but you should get the data for the model first, so I made a demo, you can check it:
Model:
public class VMTicketReply { public IEnumerable<Ticket> Tickets { get; set; } } public class Ticket { public int TicketID { get; set; } public string TicketContent { get; set; } public bool TicketIsSeen { get; set; } }
View:
@model VMTicketReply @await Html.PartialAsync("_LoginPartial.cshtml", Model.Tickets)
Partial view:
@model IEnumerable<Ticket> @foreach (var item in Model) { if (item.TicketIsSeen == false) { <a asp-action="Details" asp-route-id="@item.TicketID">@item.TicketContent</a> } }
Controller(if you use a database, then the sql connection sentence should be written in the controller)
public IActionResult Index() { var data = new VMTicketReply() { Tickets =new List<Ticket>() { new Ticket{ TicketID = 1, TicketContent = "It's a ticket", TicketIsSeen = false }, new Ticket{ TicketID = 1, TicketContent = "It's a ticket2", TicketIsSeen = true } } }; return View(data); }
Result:
Best Regards,
Jerry Cai
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 30, 2020 2:05 AM -
User1051638994 posted
Thank you for your help
I used ViewComponents instead of partials and it worked
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 30, 2020 8:53 AM
All replies
-
User475983607 posted
mspace
But always retuns ArgumentNullException: Value cannot be null. Any Idea? Thank youBecause you are specifically passing null/empty IEnumerable properties!
@await Html.PartialAsync("_LoginPartial.cshtml", new VMTicketReply())
See the C# programming guide to learn C# classes and constructors.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors
Thursday, October 29, 2020 12:19 PM -
User1051638994 posted
Thank you for your reply.
If I dont use this
new VMTicketReply()
then I get error to partial view too.
I tried this
public IEnumerable<Ticket> Tickets { get; set; } = new List<Ticket>();
I dont receive any errors but also the data are not being displayed
Thursday, October 29, 2020 12:45 PM -
User475983607 posted
I dont receive any errors but also the data are not being displayedOf course there's no data. You are not populating the model.
Getting Started Tutorials.
https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/?view=aspnetcore-3.1
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/?view=aspnetcore-3.1
Thursday, October 29, 2020 1:18 PM -
User1312693872 posted
Hi,mspace
But always retuns ArgumentNullException: Value cannot be null. Any Idea? Thank youI haven't seen any data passing code in your shared code, but you should get the data for the model first, so I made a demo, you can check it:
Model:
public class VMTicketReply { public IEnumerable<Ticket> Tickets { get; set; } } public class Ticket { public int TicketID { get; set; } public string TicketContent { get; set; } public bool TicketIsSeen { get; set; } }
View:
@model VMTicketReply @await Html.PartialAsync("_LoginPartial.cshtml", Model.Tickets)
Partial view:
@model IEnumerable<Ticket> @foreach (var item in Model) { if (item.TicketIsSeen == false) { <a asp-action="Details" asp-route-id="@item.TicketID">@item.TicketContent</a> } }
Controller(if you use a database, then the sql connection sentence should be written in the controller)
public IActionResult Index() { var data = new VMTicketReply() { Tickets =new List<Ticket>() { new Ticket{ TicketID = 1, TicketContent = "It's a ticket", TicketIsSeen = false }, new Ticket{ TicketID = 1, TicketContent = "It's a ticket2", TicketIsSeen = true } } }; return View(data); }
Result:
Best Regards,
Jerry Cai
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 30, 2020 2:05 AM -
User1051638994 posted
Thank you for your reply! I understand your logic
but now I get
'IHtmlHelper<dynamic> has no applicable method named but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
at line
@await Html.PartialAsync("_LoginPartial.cshtml", Model.Ticket)
Friday, October 30, 2020 7:02 AM -
User1051638994 posted
Thank you for your help
I used ViewComponents instead of partials and it worked
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 30, 2020 8:53 AM -
User1312693872 posted
Hi,mspace
I'm glad that you have solved your problem, I suggest you mark the answer which you think is useful, so it will help more people who have the similar problems.
Best Regards,
Jerry Cai
Friday, October 30, 2020 9:44 AM