Click here to Skip to main content
15,120,883 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In .net mvc framework entity
How do i fetch data from two table in two different dropdown list and insert this data as a foreign key in third table like i have table1 Car and table2 Carfeature and table3 CarFeatureMapping.
i have to fetch this data from table1 and table2 as a dropdown list and insert both table foreign key in table3

What I have tried:

i have model file , contoller file . irepositroy file , repository file , and html file

Controller file ...
using InventoryManagement.Models;
using InventoryManagement.Repository;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace InventoryManagement.Controllers
{
    public class CarFeatureMapController : Controller
    {
        private readonly ICarFeatureMapRepository _carfeaturemapRepository;
        private readonly ICarRepository _carRepository;
        private readonly ICarFeatureMasterRepository _carfeaturemasterRepository;

        public CarFeatureMapController(ICarFeatureMapRepository carfeaturemapRepository ,
            ICarRepository carRepository, ICarFeatureMasterRepository carfeaturemasterRepository)
        {
            _carfeaturemapRepository = carfeaturemapRepository;
            _carRepository = carRepository;
            _carfeaturemasterRepository = carfeaturemasterRepository;
        }

        public ActionResult Index()
        {
           List<CarFeatureMap> maplist = _carfeaturemapRepository.GetAllCarFeatureMap();
            return View(maplist);
        }

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(CarFeatureMap cfm)
        {
            _carfeaturemapRepository.AddCarFeatureMap(cfm);
            List<CarFeatureMap> mapList = _carfeaturemapRepository.GetAllCarFeatureMap();
            return View("Index", mapList);
        }

        public ActionResult Delete(int MapId)
        {
            _carfeaturemapRepository.Delete(MapId);
            List<CarFeatureMap> mapList = _carfeaturemapRepository.GetAllCarFeatureMap();
            return View("Index", mapList);

        }
            
        [HttpGet]
        public ActionResult Map()
        {
            List<CarFeatureMap> mapList = _carfeaturemapRepository.GetAllCarFeatureMap();
            List<Car> CarList = _carRepository.GetAllCars();
            List<CarFeatureMaster> FeatureList = _carfeaturemasterRepository.GetAllCarFeatureMasters();
            CarFeatureMap carfeatureMap = new CarFeatureMap();
            List<Car> CarsList = Car.CarList;
            List<CarFeatureMaster> FeaturesList = CarFeatureMaster.FeatureList;
            return View();
        }

        [HttpPost]
        public ActionResult Map(int carId, int FeatureId)
        {
            _carfeaturemapRepository.Map(carId, FeatureId);
            List<CarFeatureMap> mapList = _carfeaturemapRepository.GetAllCarFeatureMap();
            return View("Index", mapList);

        }

    }
}


View HTML file..
@model InventoryManagement.Models.View_Model.CarFeatureMap
<div class="container">
    <form action="Map" method="post">
        <div class="row">
            <div class="col-25">
                <label for="makename">Car Make</label>
            </div>
            <div class="col-75">
                <select id="Make" name="Make">
                    @foreach (var Car in Model.CarList)
                    { 
                        <option value="@Car.CarId">@Car.Make</option>
                    }
                </select>
            </div>
        </div>
        <div class="row">
            <div class="col-25">
                <label for="makename">Feature Description</label>
            </div>
            <div class="col-75">
                <select id="Make" name="Make">
                    @foreach (var carfeature in Model.FeatureList)
                    {
                        <option value="@carfeature.FeatureId">@carfeature.FeatureDescription</option>
                    }
                </select>
            </div>
        </div>
        

        <div>
            <input type="submit" value="submit">
        </div>
    </form>
</div>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900