IEnumerator
IEnumerator

Reputation: 2972

Create a record where the Model inherits from another Model

How do I set a models reference property correctly?

I have a Model called Platform which has a navigational property to a model called Service.

The issue I am having is with setting the value of platformToCreate.Service.

Here is a screenshot of the Platform table:

alt text http://img268.imageshack.us/img268/6394/platform.jpg

Here is a screenshot of the EDM:

alt text http://img19.imageshack.us/img19/3664/edms.jpg

Code:

[ValidateInput(true)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "id")] Platform platformToCreate)
        {

            ViewData["title"] = platformToCreate.title;
            ViewData["url"] = platformToCreate.url;
            ViewData["platform_nm"] = platformToCreate.platform_nm;
            ViewData["enabled"] = platformToCreate.enabled;

            int selectedService = int.Parse(Request.Form["service_id"].ToString());

            var cats = from c in db.Services select c;
            ViewData["service_id"] = new SelectList(cats, "id", "name", selectedService);

            if (!_service.CreatePlatform(platformToCreate))
                return View(platformToCreate);

            TempData["Msg"] = "Record Created";
            return RedirectToRoute(new { controller = "Home" });
        }

Upvotes: 0

Views: 207

Answers (1)

mhenrixon
mhenrixon

Reputation: 6278

It depends on what version of EF you are using and even if I knew what version the possibilities are still endless. :) I'll give you a few examples.

Version 1.0

public void Update(User user)
{

    using (var context = new ObjectContext(""))
    {
        user.Blog = GetBlog(user.Id);

        context.ApplyPropertyChanges("User", user);
    }
}

private Blog GetBlog(int userId)
{
    using (var context = new ObjectContext(""))
    {
        return (from u in context.Users
                where u.Id == userId
                select u).FirstOrDefault();
    }
}

In version 2 and POCO:

public class User
{
    public virtual int Id { get; set; }
    public virtual Blog Blog { get; set; }

    public void AddEntry(Entry entry)
    {
        if (Blog.Entries == null)
            Blog.Entries = new List<Entry>();

        entry.Blog = Blog;
        Blog.Entries.Add(entry);
    }
}

public class Blog
{
    public virtual int Id { get; set; }
    public virtual IList<Entry> Entries { get; set; }
}

Then of course there are a multitude of options using the EntityKey values, loading references etc. I tend to use the easiest variant at hand though.

EDIT: Ok got your question. MVC is very forgiving if you treat it right. I would start with the following: When you create your select list thats fine:

ViewData["service_id"] = new SelectList(cats, "id", "name", selectedService);

But to use this select list correctly in MVC you can actually assign it like the following:

<%= Html.DropDownList("Service.Id", (IEnumerable<SelectListItem>)ViewData["service_id"])%>

The thing to note here is Service.Id. This means that whatever value is selected in the drop down list get's assigned to Platform.Service.Id. When you persist your changes Entity Framework does the work for you.

Upvotes: 1

Related Questions