0

Good day,

I am creating an ASP.Net site with MVC 5 and Razor views. I have a Parent class and a Child class, so that a Parent can have multiple Children. I am using EF Code first and have my 2 models with my DB context in the Models folder in my project. I have created scaffolded items for both.

What I have done is on the details of a Parent, I have a list of children, using a _Partial view. What I would like to do is from the Parent details, if you click on a 'New Child' Action Link, the Create for Child is displayed with the ParentId already selected and not editable.

What woukd be the best way to implement this? Would I need to pass the data along to the controller or in ViewBag? I do not know the correct term is for this type of layout/functionality (Master-Detail view?).

I have also been on the ASP.net site, but cannot find what I am looking for.

1 Answer 1

2

You can pass the Parent Id to the Create action using the Action Link. Perhaps something like this, where "item" is the name of the local Parent variable:

@Html.ActionLink("New Child", "Create", new { id = item.Id }) 

Your Create action can then pass it along to the form in the Create view.

Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant, thanks. I actually ended up using RouteValuesDictionary as well as I required some more info passed along as well: @{ var routeValues = new RouteValueDictionary(); routeValues.Add("ParentId", Model.ParentId); routeValues.Add("Surname", Model.Surname); } @Html.ActionLink("New Child", "Create", routeValues)
For what it's worth, I think you might be able to put that inline as well, such as: @Html.ActionLink("New Child", "Create", new { ParentId = Model.ParentId, Surname = Model.Surname });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.