I am new in ASP .Net MVC
I have three table, Categories, Intents and IntentCategories.
IntentCategories table has two columns as IntentId and CategoryId. I display the names instead of Ids.
This is my IntentCategories Model.
{
public Guid Id { get; set; }
public Guid IntentId { get; set; }
public Guid CategoryId { get; set; }
[NotMapped]
public virtual string CategoryName { get; set; }
public virtual Categories Category { get; set; }
public virtual string IntentFriendlyName { get; set; }
public virtual Intents Intent { get; set; }
}
And here these are my Create methods
public IActionResult Create()
{
List<SelectListItem> selectListItemsIntents = _context.Intents.
Select(i => new SelectListItem()
{
Text = i.FriendlyName,
Value = i.Id.ToString()
}).ToList();
List<SelectListItem> selectListItemsCategories = _context.Categories.
Select(m => new SelectListItem()
{
Text = m.Name,
Value = m.Id.ToString()
}).ToList();
ViewBag.selectedListItemsIntents = selectListItemsIntents;
ViewBag.selectListItemsCategories = selectListItemsCategories;
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,IntentId,CategoryId")] IntentCategories intentCategories)
{
if (ModelState.IsValid)
{
var intent = _context.Intents.Where(x => x.Id == intentCategories.IntentId).FirstOrDefault();
intentCategories.Intent = intent;
var category = _context.Categories.Where(m => m.Id == intentCategories.CategoryId).FirstOrDefault();
intentCategories.Category = category;
intentCategories.Id = Guid.NewGuid();
_context.Add(intentCategories);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(intentCategories);
}
I have create page, I see the name of Intent and name of Category, Its Ok, I can select them. When I click the create button I get an error: Invalid column name: IntentFriendlyName and CategoryName.
How can I fix this? I use these two column just display the name instead of ID.
I edited the question make more clear. When I clik the create button CategoryID,IntentID and IntentCategoriesId are created. These are enough for me. Other null columns are not exist in my table, I use them just displaying name.