0

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.

enter image description here

6
  • Did you run migrations? The tables might not exist Commented Feb 11, 2021 at 11:25
  • I am working on existing datebase. The tables exist. Commented Feb 11, 2021 at 11:26
  • 1
    Like the error message is saying, you have a property named IntentFriendlyName in the model, but not in the database, so when it tries to insert the value of IntentFriendlyName to the column with saem name, it fails. You need to set NotMapped attribute for that field too. Commented Feb 11, 2021 at 11:49
  • I have NotMapped attribute in my IntentCategories model. You can see the above, in first code block Commented Feb 11, 2021 at 11:51
  • ooov Ok, I got the point @MatJ thanks you :) Commented Feb 11, 2021 at 11:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.