Reputation: 1
I'm farely new to MVC with only going on 2 months of experience
What I would like to know is how I can use 2 models in 1 view because the current way I'm doing it is giving me this error: System.NullReferenceException: 'Object reference not set to an instance of an object.'
These are 2 seperate models
namespace SwiftMLS.Data.DataModels;
public class FAQTipCategory : BaseDataModel
{
public string Name { get; set; }
}
namespace SwiftMLS.Data.DataModels;
public class FAQTips : BaseDataModel
{
public int? FAQTipCategoryId { get; set; }
public virtual FAQTipCategory FAQTipCategory { get; set; }
public string? Title { get; set; } = string.Empty;
public string? Description { get; set; } = string.Empty;
}
This is my controller
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SwiftMLS.Data;
using SwiftMLS.Data.DataModels;
using System.Text.Json;
namespace SwiftMLS.Web.Controllers;
public class FAQController : Controller
{
private readonly ApplicationDbContext _db;
private readonly UserManager<ApplicationUser> _userManager;
public FAQController(ApplicationDbContext context, UserManager<ApplicationUser> userManager)
{
_db = context;
_userManager = userManager;
}
public async Task<IActionResult> Index()
{
ViewData["FAQTipsDesc"] = _db.Tips.ToList();
ViewData["FAQDescription"] = _db.Descrpt;
List<FAQCategory> categories = await _db.Categories.Where(m => !m.IsDeleted).ToListAsync();
return View(categories);
}
//FAQ
[HttpGet]
public async Task<IActionResult> AddPost(FAQCategory category)
{
_db.Categories.Add(category);
await _db.SaveChangesAsync();
return RedirectToAction("Index");
}
[HttpGet]
public async Task<IActionResult> AddDescription(string Title, string Description, int FAQCategoryId)
{
FAQ faq = new FAQ
{
Title = Title,
Description = Description,
FAQCategoryId = FAQCategoryId
};
_db.Descrpt.Add(faq);
await _db.SaveChangesAsync();
return RedirectToAction("Index");
}
//FAQTip
[HttpGet]
public async Task<IActionResult> AddTip(FAQTipCategory tipCategory)
{
_db.TipsCat.Add(tipCategory);
await _db.SaveChangesAsync();
return RedirectToAction("Index");
}
[HttpGet]
public async Task<IActionResult> AddTipDescription(string Title, string Description, int FAQTipCategoryId)
{
FAQTips faqtips = new FAQTips
{
Title = Title,
Description = Description,
FAQTipCategoryId = FAQTipCategoryId
};
_db.Tips.Add(faqtips);
await _db.SaveChangesAsync();
return RedirectToAction("Index");
}
}
This is how I'm calling it in the view
@{
ViewData["Title"] = "FAQTips";
DbSet<FAQTips> Tips = ViewData["FAQTipsDesc"] as DbSet<FAQTips>;
}
@foreach (var item in ViewData["FAQTIPSDesc"] as List<FAQTipCategory>)
Upvotes: 0
Views: 40
Reputation: 25370
Make a real view model, that contains all the things that are going to need to be available on the view (I've guessed at some of the type names):
public class IndexViewModel
{
public List<FAQTip> FaqTips { get; set; }
public List<FAQDescription> FaqDescriptions { get; set; }
public List<FAQTipCategory > Categories { get; set; }
}
Views often use aggregate information from different tables in the database. It's good to create a ViewModel that is scoped to just the web project, that contains all the data the view will need.
Upvotes: 1