1

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.

PIF Details view with Internal Comments model data showing

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:Internal Comments ProjectID field not populating

10
  • I don't think your ActionLink can be compiled, especially the Model.InternalComments, seems like you are missing defining the property, should be: InternalComments = Model.InternalComments Commented Sep 15 at 8:45
  • And another issue is that you need to pass an enumerable to asp-items which will be render as dropdown options, but now based on your requirement you will pass an integer as ViewBag.ProjectID. asp-items="ViewBag.ProjectID" Commented Sep 15 at 8:47
  • Thank you for your help. I'm now getting this error when I click on the Add a New Internal Comment button: RuntimeBinderException: Cannot implicitly convert type 'int' to 'System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Mvc.Rendering.SelectListItem>'. I added "InternalComments =" before "Model.InternalComments" in the ActionLink but I got an error using: <select asp-for="ProjectID" class="form-control" asp-items=ViewBag.Project.asp-items="ViewBag.ProjectID"></select>. Commented Sep 15 at 9:12
  • Copilot suggested the following but that gives the same error: <select asp-for="ProjectID" class="form-control" asp-items="ViewBag.ProjectID"></select> Commented Sep 15 at 9:12
  • 1
    Have you try adding this line ViewData["ProjectIDList"] = new SelectList(_context.PIF!.ToList(), "ProjectID", "ProjectID"); in your Create method? Commented Sep 15 at 10:12

1 Answer 1

0

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I'm afraid I made an error in my original question. I added the wrong code for the button. I've updated the question. It takes me to the Internal Comments model Create.

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.