我必须添加一个dropdownlist到我现有的项目“moviesite”,我有一个主表“moviestable”添加,编辑和删除电影。现在添加了一个新的表“流派”来添加电影流派。第二个表由两列组成-1,“genreid设置为primarykey和identity,第二列是“name”,由动作、戏剧、喜剧等类型组成,并在主表中添加了一列“genrerefid”,设置为外键。现在我想访问main priject中下拉列表的辅助表列。所以我创建了一个新的模型类“genreclass.cs”,并添加了dbcontext。现在我开始学习如何在模型、控制器和索引中实现它。
NewmovieClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using Moviesite.Enums;
using System.ComponentModel.DataAnnotations.Schema;
namespace Moviesite.Models
{
public class NewmovieClass
{
[Key]
public int Movieid { get; set; }
[Required]
public string Movietitle { get; set; }
[Required]
public string Description { get; set; }
[Required]
public string Storyline { get; set; }
public int Year { get; set; }
public DateTime Releasedate { get; set; }
public int Runtime { get; set; }
[Column(TypeName="nvarchar(50)")]
public Mvetypenum MovieType { get; set; }
public int GenreRefId { get; set; }
[ForeignKey("GenreRefId")]
public GenreClass Genre { get; set; }
}
}
GenreClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using Moviesite.Enums;
using System.ComponentModel.DataAnnotations.Schema;
namespace Moviesite.Models
{
public class GenreClass
{
[Key]
public int GenreId { get; set; }
public string Name { get; set; }
public ICollection<NewmovieClass> newmovieClasses { get; set; }
}
}
MveController.cs
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Moviesite.Models;
using Microsoft.EntityFrameworkCore;
namespace Moviesite.Controllers
{
public class MveController : Controller
{
private readonly ApplicationDbContext _db;
public MveController(ApplicationDbContext db)
{
_db = db;
}
public IActionResult Index()
{
var displaydata = _db.Moviestable.ToList();
return View(displaydata);
}
[HttpGet]
public async Task<IActionResult> Index (String Mvesearch)
{
ViewData["Getmoviedetails"] = Mvesearch;
var mvequery = from x in _db.Moviestable select x;
if (!string.IsNullOrEmpty(Mvesearch))
{
mvequery = mvequery.Where(x => x.Movietitle.Contains(Mvesearch) || x.Description.Contains(Mvesearch));
}
return View(await mvequery.AsNoTracking().ToListAsync());
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Create([Bind("Movieid,Movietitle,Description,Storyline,Year,Releasedate,Runtime,MovieType")] NewmovieClass nmc)
{
if (ModelState.IsValid)
{
_db.Add(nmc);
await _db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(nmc);
}
public async Task< IActionResult> Edit(int? id)
{
if(id==null)
{
return RedirectToAction("Index");
}
var getmvedetails = await _db.Moviestable.FindAsync(id);
return View(getmvedetails);
}
[HttpPost]
public async Task<IActionResult> Edit(NewmovieClass mc)
{
if (ModelState.IsValid)
{
_db.Update(mc);
await _db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(mc);
}
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return RedirectToAction("Index");
}
var getmvedetails = await _db.Moviestable.FindAsync(id);
return View(getmvedetails);
}
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return RedirectToAction("Index");
}
var getmvedetails = await _db.Moviestable.FindAsync(id);
return View(getmvedetails);
}
[HttpPost]
public async Task<IActionResult> Delete(int id)
{
var getmvedetails = await _db.Moviestable.FindAsync(id);
_db.Moviestable.Remove(getmvedetails);
await _db.SaveChangesAsync();
return RedirectToAction("Index");
}
}
}
AppDbcontext
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace Moviesite.Models
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<NewmovieClass> Moviestable { get; set; }
public DbSet<GenreClass> Genre { get; set; }
}
}
1条答案
按热度按时间t3psigkw1#
第一步获取和列出流派,并在
viewbag
```public IActionResult Create()
第三步:添加
GenreRefId
在Post
方法