質問

i'm trying to realize my CRUD operations in Entity Framework using DbContext

i have a class called "offreLocation" inherit from a supper class called "publication"

i do not get any error, but after clicking in the create button, i still have the create view although i specified a redirection to the list view after creating.

here my Create Function

  // POST: /OffreLocation/Create
    [HttpPost]
    public ActionResult CreateOffreLocation(OffreLocation offreLocation)
    {
        try
        {
            db.PublicationSet.Add(offreLocation);
            db.SaveChanges();

            return RedirectToAction("ListOffreLocation");
        }
        catch
        {
            return View();
        }
    }

My Create 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>

for more precision here my Model Container

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" Model

    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" Model

 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; }
}
役に立ちましたか?

解決

This is because you are getting an Exception at either of these 2 lines:

db.PublicationSet.Add(offreLocation);
db.SaveChanges();

which takes you to the catch block. which again returns the same view.

catch
{
    return View();
}

Try debugging your code. Place breakpoint at these 2 lines. and find what is causing it to throw exception. (It's most probably due to some Database related issue, wrong data etc.)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top