I come to asp.net MVC 4 from the background in Rails, and I am having some problems with how to handle nested models.
I am using Entity Framework 4.3 with the CodeFirst approach.
The application is quite simple and has an Install model that has many Franchises . And he has a franchise model - which belongs to the sets.
The POCO classes were created along with the corresponding navigation properties, then I modeled the controllers with the views, and then generated the database.
Here are the classes:
Set
namespace FranchiseManagerTest.Models
{
public class Set
{
[Key]
public int SetID { get; set; }
public string SetName { get; set; }
public string MainAddress { get; set; }
public string TimeZone { get; set; }
public virtual ICollection<Franchise> Franchises { get; set; }
}
}
Franchise
namespace FranchiseManagerTest.Models
{
public class Franchise
{
[Key]
public int FranchiseID { get; set; }
public int SetID { get; set; }
public string FranchiseName { get; set; }
public string FranchiseNumber { get; set; }
public virtual Set Set { get; set; }
}
}
Then I created some franchise and franchise tests.
Here is the main view:

, , , . , , Set.
, , , , "" :

ActionLink, :
@Html.ActionLink("New Franchise", "Create", "Franchise", null, new { @class = "btn btn-large btn-primary" })
, :

, , , , Set Model, - , .
- - , .
, :

, , :
<div class="editor-label">
@Html.LabelFor(model => model.SetID, "Set")
</div>
<div class="editor-field">
@Html.DropDownList("SetID", String.Empty)
@Html.ValidationMessageFor(model => model.SetID)
</div>
:
SetID ActionLink:
@Html.ActionLink("New Franchise", "Create", "Franchise", Model.SetID, new { @class = "btn btn-large btn-primary" })
String.Empty Create View:
<div class="editor-label">
@Html.LabelFor(model => model.SetID, "Set")
</div>
<div class="editor-field">
@Html.DropDownList("SetID", Model.SetID)
@Html.ValidationMessageFor(model => model.SetID)
</div>
.
( ?)
"Create Franchise View" , , URL-:

, Set , , URL- :
localhost:52987/Set/1/Franchise/Create
- .NET MVC?
!
Global.asax, :
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Set", action = "Index", id = UrlParameter.Optional }
);
}
- GET POST Create :
public ActionResult Create()
{
ViewBag.SetID = new SelectList(db.Sets, "SetID", "SetName");
return View();
}
[HttpPost]
public ActionResult Create(Franchise franchise)
{
if (ModelState.IsValid)
{
db.Franchises.Add(franchise);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.SetID = new SelectList(db.Sets, "SetID", "SetName", franchise.SetID);
return View(franchise);
}
, .