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:
You can send the ProjectID value of the current PIF with the asp-route attribute as query string.
<a asp-controller="InternalComments"
asp-action="Create"
asp-route-projectID="@item.ProjectID"
class="btn btn-primary" role="button">
Add a New Comment
</a>
Next, in your [HttpGet] InternalComments Create action, obtains the projectID from the query string via [FromQuery]. For model binding, you should assign the projectID into model as below:
[HttpGet]
public IActionResult Create([FromQuery] int projectID)
{
// Assume your model class is InternalComment
InternalComment model = new InternalComment();
model.ProjectID = projectID;
// Following with your logic
return View(model);
}