我正在尝试加入两种模式
Brand of car
和Model of car
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace TriglavOsiguranje.Models
{
public class ModeliVozila
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(20)]
public string Ime { get; set; }
public virtual MarkeVozila MarkeVozila { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace TriglavOsiguranje.Models
{
public class MarkeVozila
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(30)]
public string Ime { get; set; }
public virtual ICollection<ModeliVozila> ModeliVozila { get; set; }
}
}
在我的Create.cshtml中,我想显示下拉菜单以选择将哪个汽车模型连接到品牌 就像在沃尔沃->汽车品牌 S60->汽车模型 到目前为止,我在控制器中所做的工作都是尝试使用
传递IDViewBag.MarkeVozilaId = new SelectList(_db.MarkeVozila, "Id", "Name", modeliVozila.MarkeVozila);
并在我的“创建”视图中显示,但是我得到“对象”未设置为对象的实例 到目前为止,这是我的控制器
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using TriglavOsiguranje.Data;
using TriglavOsiguranje.Models;
namespace TriglavOsiguranje.Areas.Admin.Controllers
{
[Area("Admin")]
public class ModeliVozilaController : Controller
{
private readonly ApplicationDbContext _db;
public ModeliVozilaController(ApplicationDbContext db)
{
_db = db;
}
public IActionResult Index()
{
return View(_db.Modeli.Include(m => m.MarkeVozila).ToList());
}
//GET Create Action Metod
public IActionResult Create()
{
ViewBag.MarkeVozilaId = new SelectList(_db.MarkeVozila, "Id", "Name");
return View();
}
//POST Create Action Metod
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(ModeliVozila modeli)
{
if (ModelState.IsValid)
{
_db.Add(modeli);
await _db.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewBag.MarkeVozilaId = new SelectList(_db.MarkeVozila, "Id", "Name", modeli.MarkeVozila);
return View(modeli);
}
//GET Edit Action Metod
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var modeliVozila = await _db.Modeli.FindAsync(id);
if (modeliVozila == null)
{
return NotFound();
}
ViewBag.MarkeVozilaId = new SelectList(_db.MarkeVozila, "Id", "Name");
return View(modeliVozila);
}
//POST Edit Action Metod
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, ModeliVozila modeliVozila)
{
if (id != modeliVozila.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
_db.Update(modeliVozila);
await _db.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewBag.MarkeVozilaId = new SelectList(_db.MarkeVozila, "Id", "Name", modeliVozila.MarkeVozila);
return View(modeliVozila);
}
任何人都知道如何以简短的方式执行此操作,我不希望如此复杂 基本上只显示汽车品牌
答案 0 :(得分:1)
我创建了一个小示例项目,您可以在其中看到如何完成该项目。我再也看不到您的代码中的任何错误,但也许您看到了不同:
模型(Marka):
canActivate(): Promise<boolean> {
return new Promise((resolve) =>
this.authService.getUserProfile().subscribe((res: any)=>{
resolve(res.data.id == 2);
});
}
模型(模型):
namespace WebApplication22.Models
{
public class Marka
{
public int Id { get; set; }
public string Name { get; set; }
}
}
查看(创建):
namespace WebApplication22.Models
{
public class Model
{
public int Id { get; set; }
public int MarkaId { get; set; }
[ForeignKey(nameof(MarkaId))]
public virtual Marka Marka { get; set; }
}
}
控制器(POST创建):
@model WebApplication22.Models.Model
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Model</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.MarkaId, "MarkaId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("MarkaId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.MarkaId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
希望对您有所帮助!