我正在尝试使用DbContext
在Entity Framework中实现我的CRUD操作我有一个名为“offreLocation”的类继承自名为“publication”的晚餐类
我没有收到任何错误,但点击创建按钮后,我仍然有创建视图,虽然我在创建后指定了重定向到列表视图。
这里是我的创建功能
// POST: /OffreLocation/Create
[HttpPost]
public ActionResult CreateOffreLocation(OffreLocation offreLocation)
{
try
{
db.PublicationSet.Add(offreLocation);
db.SaveChanges();
return RedirectToAction("ListOffreLocation");
}
catch
{
return View();
}
}
我的创建视图:
@model COM.MENNILIK.Models.OffreLocation
@{
ViewBag.Title = "CreateOffreLocation";
}
<h2 style="text-align:center">Nouvelle offre de location </h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Publication_ID, new { @class = "control-label col-md-2" })
<div class="col-lg-1">
@Html.EditorFor(model => model.Publication_ID)
@Html.ValidationMessageFor(model => model.Publication_ID)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Publication_Statut, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Publication_Statut)
@Html.ValidationMessageFor(model => model.Publication_Statut)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Publication_Meublee, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Publication_Meublee)
@Html.ValidationMessageFor(model => model.Publication_Meublee)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Publication_Descriptif, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Publication_Descriptif)
@Html.ValidationMessageFor(model => model.Publication_Descriptif)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Publication_ContactParAgence, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Publication_ContactParAgence)
@Html.ValidationMessageFor(model => model.Publication_ContactParAgence)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Publication_Maps_Latitude, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Publication_Maps_Latitude)
@Html.ValidationMessageFor(model => model.Publication_Maps_Latitude)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Publication_Maps_Longitude, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Publication_Maps_Longitude)
@Html.ValidationMessageFor(model => model.Publication_Maps_Longitude)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OffreLocation_TypeLog, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OffreLocation_TypeLog)
@Html.ValidationMessageFor(model => model.OffreLocation_TypeLog)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OffreLocation_Sante, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OffreLocation_Sante)
@Html.ValidationMessageFor(model => model.OffreLocation_Sante)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OffreLocation_Loyer, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OffreLocation_Loyer)
@Html.ValidationMessageFor(model => model.OffreLocation_Loyer)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OffreLocation_DateDisponibilite, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OffreLocation_DateDisponibilite)
@Html.ValidationMessageFor(model => model.OffreLocation_DateDisponibilite)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OffreLocation_Superficie, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OffreLocation_Superficie)
@Html.ValidationMessageFor(model => model.OffreLocation_Superficie)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OffreLocation_NbreChambre, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OffreLocation_NbreChambre)
@Html.ValidationMessageFor(model => model.OffreLocation_NbreChambre)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OffreLocation_NbrePieces, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OffreLocation_NbrePieces)
@Html.ValidationMessageFor(model => model.OffreLocation_NbrePieces)
</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", "ListOffreLocation")
</div>
我的模型容器
更精确public partial class Model1Container : DbContext
{
public Model1Container()
: base("name=Model1Container")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Utilisateur> UtilisateurSet { get; set; }
public DbSet<Abonnement> AbonnementSet { get; set; }
public DbSet<AbonnementHistorique> AbonnementHistoriqueSet { get; set; }
public DbSet<ColocataireIdeal> ColocataireIdealSet { get; set; }
public DbSet<Publication> PublicationSet { get; set; }
public DbSet<Quartier> QuartierSet { get; set; }
public DbSet<Ville> VilleSet { get; set; }
public DbSet<RegionProvince> RegionProvinceSet { get; set; }
public DbSet<Photo> PhotoSet { get; set; }
public DbSet<MessageLocation> MessageLocationSet { get; set; }
public DbSet<MessageColocation> MessageColocationSet { get; set; }
}
“publication”模型
public partial class Publication
{
public Publication()
{
this.Photo = new HashSet<Photo>();
}
public int Publication_ID { get; set; }
public string Publication_Statut { get; set; }
public bool Publication_Meublee { get; set; }
public string Publication_Descriptif { get; set; }
public bool Publication_ContactParAgence { get; set; }
public double Publication_Maps_Latitude { get; set; }
public double Publication_Maps_Longitude { get; set; }
public virtual Quartier Quartier { get; set; }
public virtual ICollection<Photo> Photo { get; set; }
}
“offreLocation”模型
public partial class OffreLocation : Publication
{
public OffreLocation()
{
this.Locataire = new HashSet<Locataire>();
this.DemandeLocation = new HashSet<DemandeLocation>();
this.DemandeLocation1 = new HashSet<DemandeLocation>();
this.DemandeLocation2 = new HashSet<DemandeLocation>();
this.DemandeLocation3 = new HashSet<DemandeLocation>();
}
public string OffreLocation_TypeLog { get; set; }
public string OffreLocation_Sante { get; set; }
public double OffreLocation_Loyer { get; set; }
public System.DateTime OffreLocation_DateDisponibilite { get; set; }
public double OffreLocation_Superficie { get; set; }
public short OffreLocation_NbreChambre { get; set; }
public short OffreLocation_NbrePieces { get; set; }
public virtual ICollection<Locataire> Locataire { get; set; }
public virtual Proprietaire Proprietaire { get; set; }
public virtual ICollection<DemandeLocation> DemandeLocation { get; set; }
public virtual ICollection<DemandeLocation> DemandeLocation1 { get; set; }
public virtual ICollection<DemandeLocation> DemandeLocation2 { get; set; }
public virtual ICollection<DemandeLocation> DemandeLocation3 { get; set; }
}
答案 0 :(得分:1)
这是因为你在这两行中的任何一行都得到了一个例外:
db.PublicationSet.Add(offreLocation);
db.SaveChanges();
将您带到catch
区块。它再次返回相同的视图。
catch
{
return View();
}
尝试调试代码。将breakpoint
放在这两行。并找到导致它抛出异常的原因。 (这很可能是由于某些与数据库相关的问题,错误的数据等。)