views:

37

answers:

2

I'm trying to add a webform that allows the user to add a database entry with a specific foreign key.

I'm creating the link like this

<%= Html.ActionLink("Edit", "EditSub", new { id = id }) %>

and the resulting URL is http://localhost:3015/TumourGroup/CreateSub/2 (where 2 is the id I passed to the actionlink earlier). The question is, how do I retrieve the value of id from the URL? I'm using it to grab the "main" table so that I can create a new table entry that has a foreign key pointing to the "main" table.

A: 

Have your controller function CreateSub take in int id

public ActionResult CreateSub (int id)

If you want to go to the form with this id, then post with a different set of data, you'd need two functions, differentiated by

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult CreateSub (int id)

The get is for navigating to your entry form, the post is called when the form posts.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateSub (TumourGroupSubcategory tumourSubgroupToCreate)

BELOW IS RESPONDING TO CLARIFICATION IN COMMENTS:

Well, if you forego the strongly typed view, you can just do

ViewData["id"] = id;
ViewData["subGroupToCreate"] = ...

Alternatively you can do it in the second form on the client side with Javascript or Jquery

Russell Steen
Right, I am, but the way MVC sets it up is that there's two createsub methods, one that takes (int id) and one with (TumourGroupSubcategory tumourSubgroupToCreate), where the tumourSubgroupToCreate is being passed with the webform, and I have no idea how to get pass id to the second method since the first only has return View() as the return type.
sslepian
So you want to call CreateSub and pass it the parent ID. Then you want to post from CreateSub when they've finished to create the sub. Am i understanding that correctly?
Russell Steen
Basically, the initial CreateSub(int id) { return View() } will display the webform, and hitting submit on the webform calls CreateSub(TumourGroupSubcategory tumourSubgroupToCreate), and I'm looking for a way to pass the id to the second form without having it as part of the TumourGroupSubcategory class.
sslepian
A: 

Assuming that TumourGroup is the name of a controller, and you have a route that looks something like this:

routes.MapRoute(
    "Default",
    "{controller}, {action}, {id}",
    new { controller="Home", action="Index", id="" }
)

Then in your TumourGroup controller, you just need a controller method that looks like this:

public ActionResult CreateSub (int id)
{
    // blah
}  

The parameter id will contain your id from the Url.

EDIT: To include the id when you are submitting a form:

public ActionResult CreateSub (TumourGroupSubcategory tumourSubgroupToCreate)
{
    // blah
}  
  1. Add the id as a property to your TumorGroupSubcategory class.

  2. In the form view you are submitting, include a hidden field that is named the same as the id in your TumorGroupSubcategory class, and populate it with your id field.

When your user submits the form, the Model Binder will pick up the field, and put it into the id property of tumourSubgroupToCreate automatically.

Robert Harvey
TumourGroupSubcategory is auto-generated from a DB table, so I'm not sure if I can add new things to it. It has TumourGroupID as a foreign key into TumourGroup already, but I can't put an int into there I believe, since when MVC creates the class it wants TumourGroupID to contain a reference to an actual TumourGroup object.
sslepian
I assume you're using Linq to SQL?
Robert Harvey
I'm using the ADO.NET entity data model, not Linq
sslepian
If the relationship of `TumourGroupSubcategory` to the ID you're trying to capture is important, said ID should *already* be present in your `TumourGroupSubcategory` class. Just use *that* id.
Robert Harvey
If entity has relations, you have to use DTO for show/edit/create.
cem
I'm not 100% sure I'm being clear. I have TumourGroupSubcategory(id, name, ordering, tumourgroup_id) as my table, and tumourgroup_id is an int that's a foreign key into TumourGroup(id, name). However, when I build a model, tumourgroup_id becomes a TumourGroup rather than an int. Thus, when adding a new TumourSubgroup, I need to pass an integer, at which point the controller can use that ID to query the collection of TumourGroups for the correct one to insert.
sslepian
OK, well if your Entity Framework objects are not providing you enough flexibility, you may need to create a ViewModel object. See here for more information on this: http://nerddinnerbook.s3.amazonaws.com/Part6.htm
Robert Harvey
Yup its correct, you cannot directly assign it to entity-framework. So, If you want to get ID, post in form or querystring and Robert's right, two options, you can use NerdDinner in controller class or use DTO in repository.
cem