首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >实体框架核中下拉式外键的关联

实体框架核中下拉式外键的关联
EN

Stack Overflow用户
提问于 2022-04-11 06:23:54
回答 1查看 173关注 0票数 0

我正在建立一个简单的大学管理系统,在那里我有两个模型(现在)。下面是我的模型类。

代码语言:javascript
运行
复制
public class FacultyModel
{
    [Key] 
    public int s_no { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string? file { get; set; }
    public string? hod { get; set; }
    public ICollection<ProgramModel> ProgramModels { get; set; }
}

public class ProgramModel
{
    [Key] 
    public int s_no { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string? file { get; set; }
   
    public string type { get; set; }
    public string system { get; set; }
    public string? director { get; set; }
    public int sem_year { get; set; }
    [ForeignKey("fid")]
    public FacultyModel faculty { get; set; }  
    public int fid { get; set; }
}

我已经完成了教员的CRUD操作。现在,在插入程序(程序模型)时,我希望用户从下拉列表或选择列表中选择一个教员,所选教师的密钥将在程序模型的外键中设置。我被困在这个

以下是我的教员模型控制器

代码语言:javascript
运行
复制
public class AdminFacultyController : Controller
{
    private readonly DataContext _context;

    public AdminFacultyController(DataContext context, IWebHostEnvironment webHostEnvironment)
    {
        _context = context;
        _webHostEnvironment = webHostEnvironment;
    }

    private readonly IWebHostEnvironment _webHostEnvironment;

    // GET
    public async Task<string> UploadImage(string folderpath, IFormFile file)
    {
        folderpath += file.FileName;
        string serverFolder = Path.Combine(_webHostEnvironment.WebRootPath, folderpath);
        await file.CopyToAsync(new FileStream(serverFolder, FileMode.Create));
        return "/" + folderpath;
    }

    public IActionResult Index()
    {
        var data = _context.FacultyModels.ToList();
        ViewBag.data = data;
        return View();
    }

    public IActionResult AddFaculty()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> AddFaculty(FacultyModel facultyModel, IFormFile file)
    {
        string folder = "file/";
        facultyModel.file = await UploadImage(folder, file);
        _context.FacultyModels.Add(facultyModel);
        _context.SaveChanges();
        return RedirectToAction("Index");
    }

    public async Task<IActionResult> UpdateFaculty(int id)
    {
        var facultyModel= await _context.FacultyModels.FindAsync(id);
        ViewBag.data = facultyModel;
        return View(facultyModel);
        TempData["ID"] = id;
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> UpdateFaculty( int id, FacultyModel facultyModel, IFormFile? file, string name, string description)
    {
        if (file == null)
        {
            var faculty = _context.FacultyModels.Where(f => f.s_no == id).FirstOrDefault();
            faculty.name = facultyModel.name;
            faculty.description = description;
            await _context.SaveChangesAsync();
        }
        else
        {
            string folder = "file/";
            facultyModel.file = await UploadImage(folder, file);
            _context.FacultyModels.Update(facultyModel);
            await _context.SaveChangesAsync();
        }

        return RedirectToAction("Index");
    }

    public IActionResult AppointHod()
    {
        return View();
    }

    public IActionResult UpdateHod()
    {
        return View();
    }

    public IActionResult DeleteFaculty(int id)
    {
         var data = _context.FacultyModels.Find(id);
         _context.FacultyModels.Remove(data);
         return RedirectToAction("Index");
    }
}

下面是我的观点,其中包含了教师的选择列表。

代码语言:javascript
运行
复制
<form>
                         <div class="form-group">
                             <label for="input-1">Type</label>
                             <select class="form-control" id="input-1" placeholder="Enter type" name="type" required list="faculty">
                             <datalist id="faculty">
                                 <option > Bachelor </option>
                                 <option > Master</option>
                             </datalist>
                             </select>
                         </div>
                         
                         <div class="form-group">
                             <label for="input-2">Faculty</label>
                             <input type="text" class="form-control" id="input-2" placeholder="Enter semester/year" name="faculty" required list="teacher">
                             <datalist id="teacher">
                                 <option value="Boston"/>
                                 <option value="Cambridge"/>
                             </datalist>
                         </div>
                         
                         <div class="form-group">
                             <label for="input-3">Program Name</label>
                             <input type="text" class="form-control" id="input-3" placeholder="Enter Name" name="name" required>
                         </div>
                         <div class="form-group">
                             <label for="input-4">Description</label>
                             <input type="text" class="form-control" id="input-4" placeholder="Enter Description" name="description" required>
                         </div>
                         <div class="form-group">
                             <label for="input-5">File(syllabus)</label>
                             <input type="file" class="form-control" id="input-5" name="file">
                         </div>
                         <div class="form-group">
                             <div class="form-group">
                                 <label for="input-6">System</label>
                                 <select class="form-control" id="input-6" placeholder="Enter type" name="system" required list="system">
                                     <datalist id="system">
                                         <option > Yearly </option>
                                         <option > Semester</option>
                                     </datalist>
                                 </select>
                             </div>
                             <div class="form-group">
                                 <label for="input-7">Number of year/sem</label>
                                 <input type="number" class="form-control" id="input-7" placeholder="Enter number of year/sem" name="yearsem" required>
                             </div>
                             <button type="submit" class="btn btn-light px-5"> Add</button>
                         </div>
</form>

我只想用教员的名字填充选择列表,并将选定的教员的s_no作为外键插入到程序模型中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-11 09:15:34

下面是一个工作演示,你可以参考它。

ProgramModel

代码语言:javascript
运行
复制
 public class ProgramModel
    {
        [Key]
        public int ProgramModels_no { get; set; }
        public string name { get; set; }
        public string description { get; set; }
        public string? file { get; set; }

        public string type { get; set; }
        public string system { get; set; }
        public string? director { get; set; }
        public int sem_year { get; set; }
       
        [ForeignKey("FacultyModels_no")]
        public virtual FacultyModel FacultyModel { get; set; }
        public int FacultyModels_no { get; set; }
    }

FacultyModel

代码语言:javascript
运行
复制
public class FacultyModel
    {
        [Key]
        public int FacultyModels_no { get; set; }
        public string name { get; set; }
        public string description { get; set; }
        public string? file { get; set; }
        public string? hod { get; set; }
        public virtual ICollection<ProgramModel> ProgramModels { get; set; }
    }

在ProgramModel的Controller中,我在create中添加了以下代码:

代码语言:javascript
运行
复制
 ViewData["Faculty"] = new SelectList(_context.Set<FacultyModel>(), "FacultyModels_no", "name")

在create视图中,我添加如下所示:

代码语言:javascript
运行
复制
 <div class="form-group">
      <label asp-for="FacultyModels_no" class="control-label"></label>
      <select asp-for="FacultyModels_no" class ="form-control" asp-items="ViewBag.Faculty"></select>
 </div>

结果:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71823429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档