ASP.NET MVC 4 - Nested Models - How to set up the correct associations?

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:

enter image description here

, , , . , , Set.

, , , , "" :

enter image description here

ActionLink, :

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

, :

enter image description here

, , , , Set Model, - , .

- - , .

, :

enter image description here

, , :

       <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-:

enter image description here

, 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 :

 //
    // GET: /Franchise/Create

    public ActionResult Create()
    {
        ViewBag.SetID = new SelectList(db.Sets, "SetID", "SetName");
        return View();
    }

    //
    // POST: /Franchise/Create

    [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);
    }

, .

+5
2

, , :

:

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

:

<div class="editor-label">
    @Html.LabelFor(model => model.SetID, "Set")
</div>
<div class="editor-field">
    @Html.DropDownListFor(model => model.SetID, (SelectList)ViewBag.FranchiseSets)
    @Html.ValidationMessageFor(model => model.SetID)
</div>

SetID Create ActionMethod , . , DropDownList, .

public ActionResult Create(int SetID)
{
    ViewBag.FranchiseSets = new SelectList(...);
    var franchise = new Franchise { SetID = SetID };
    return View(franchise);
}
+3

, , :

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

SetID, MVC , "SetID".

PS - Set (FranchiseSet FranchiseGroup ..), ? Set .

+1

All Articles