I have a Details view (PIF model) that contains views of several models at the bottom. Screenshot shows the Internal Comments model data underneath the last row of PIF model data.
The PIF model has a primary key called ProjectID. The ProjectID is a foreign key in other models.
Currently, when you click on 'Add a New Comment' it takes you to the Internal Comments Create view correctly but the ViewBag.ProjectID defaults to '1'.
If the ProjectID in the PIF Details view is, for example, 3184, then I want the 'Add a New Comment' button to take you to the Create view of Internal Comments (which is does do correctly) and populate the ProjectID field with 3184.
Thanks for any help achieving this.
UPDATED CODE: Here is the code behind the 'Add a New Comment' button:
@Html.ActionLink(
"Add a New Comment",
"Create", // controller action
"InternalComments", // controller
new { ProjectID = Model.ProjectID, Model.InternalComments }, // action parameters aka route values
new { @class = "btn btn-info" })
And here is an example Create.cshtml view where the ProjectID needs to automatically populate with value from the PIF details (currently set as a viewbag):
<div class="form-group">
<label asp-for="ProjectID" class="control-label"></label>
<select asp-for="ProjectID" class ="form-control" asp-items="ViewBag.ProjectID"></select>
</div>
Edit to help clarify: Internal Comments Create.cshtml
<div class="ProjID">
<div class="row justify-content-start">
<div class="col">
<div class="form-group">
<label asp-for="ProjectID" class="control-label"></label>
<select asp-for="ProjectID" class="form-control" asp-items="ViewBag.ProjectIDList"></select>
</div>
</div>
</div>
</div>
Internal Comments Controller (altered due to feedback):
// GET: InternalComments/Create
[HttpGet]
public IActionResult Create([FromQuery] int projectID)
{
//ViewData["ProjectID"] = new SelectList(_context.PIF, "ProjectID", "ProjectID");
InternalComments model = new InternalComments();
model.ProjectID = projectID;
ViewData["ProjectID"] = projectID;
return View();
}
PIF Details.cshtml
@Html.ActionLink(
"Add a New Comment",
"Create", // controller action
"InternalComments", // controller
new { ProjectID = Model.ProjectID, InternalComments = Model.InternalComments }, // action parameters aka route values
new { @class = "btn btn-info" })
Image is what happens when you click on the ProjectID field:

Model.InternalComments, seems like you are missing defining the property, should be:InternalComments = Model.InternalCommentsasp-itemswhich will be render as dropdown options, but now based on your requirement you will pass an integer asViewBag.ProjectID.asp-items="ViewBag.ProjectID"ViewData["ProjectIDList"] = new SelectList(_context.PIF!.ToList(), "ProjectID", "ProjectID");in yourCreatemethod?