Reputation: 1939
Actually I'm working on simple project MVC4 + EF in which I have relation application-license. On application may has zero or more licenses, but every license has only one application. I've noticed some problem with operation edit/create new license. I past part of my code.
My entity domain definitions:
public class Application
{
public Application()
{
Licenses = new List<License>();
}
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ApplicationID { get; set; }
public string Name { get; set; }
public List<License> Licenses { get; set; }
}
public class License
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[HiddenInput(DisplayValue = false)]
public Guid LicenseID { get; set; }
[Required]
public string Name { get; set; }
public Version Version { get; set; }
public Application Application { get; set; }
}
Definition one to many between application and licenses:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Application>()
.HasMany(app => app.Licenses)
.WithRequired()
.Map(conf => conf.MapKey("OwnerID"))
.WillCascadeOnDelete(false);
modelBuilder.Entity<License>()
.HasRequired(lic => lic.Application)
.WithMany()
.Map(conf => conf.MapKey("ApplicationID"))
.WillCascadeOnDelete(false);
}
Action in License controller:
public ActionResult Create(Guid applicationID)
{
var application = repositoryApps.Applications.FirstOrDefault(a => a.ApplicationID == applicationID);
var license = new License {Application = application};
application.Licenses.Add(license);
return View("Edit", license);
}
public ActionResult Edit(License license)
{
return View(license);
}
[HttpPost]
public ActionResult Save(License license)
{
if(ModelState.IsValid)
{
repositoryLics.SaveLicense(license);
TempData["message"] = string.Format("{0} has been saved", license.Name);
return RedirectToAction("Index", "Application");
}
else
{
return View("Edit", license);
}
}
Edit view for fincense:
@using (Html.BeginForm("Save", "License"))
{
@Html.EditorForModel(Model);
<input type="submit" value="Save" class="btn btn-primary"/>
@Html.ActionLink("Cancel", "Index", "Application", null, new {@class = "btn"})
}
I've problem in situation when I'm creating new license. I'm invoking action Create and pass to it parent ApplicationID. After some preparation I'm passing license instance to Edit view in which is possible to edit new created object. In Edit view (for licenses) everything seems to be OK. I have a license with not null field Application (in which I have collection of licenses). When I submit edit form, then I'm back to action Save(License license). Unfortunately in this place property Application is equal to null, so I miss relation between new created license and parent application.
Has anybody any suggestions how can I avoid this problem? How should I store new created licenses objects? I'll be thankful for any hints or advices.
Regards,
Grzegorz
Upvotes: 1
Views: 767
Reputation: 14074
I don't see ApplicationID
property in your License
class !!??
you should add this property and add it so in the EditorTemplate maybe as hidden if you want.
BTW add your License
EditorTemplate
to your question to let us know about it.
Upvotes: 1