0

I have already created a view that displays data in a razor page (5 columns)

studentFirstName,  StudentLastName, StudentId,  CourseId, Grade

It also displays a button (instead of default Edit link) in each row

@Html.ActionLink("Grade", "Courses", "Students", null, new { @class = "btn btn-success" })

What I would like to do is when the button is clicked, it will navigate to new view (newPage) where a form is displayed (two fields, save button) and gets auto-populated according to the row it was click. What I have done

  1. Added IActionResult

    [HttpGet]
    public IActionResult Courses(long id)
    {
        // get the courses from the collection
        var Cor = Courselist.Where(c=>c.CourseId == id).FirstOrDefault();
        return View(Cor);
    }
    
  2. The razor page looks like this (two fields to auto populate CourseId and Grade)

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
            <h4>Update Clinical Status</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            @Html.HiddenFor(model => model.CourseId)
    
            <div class="form-group">
                @Html.LabelFor(model => model.CourseId, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.CourseId, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.CourseId, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Grade, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Grade, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Grade, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Save" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
    

I get the labels and the text fields but not the data (CourseId, Grade) from that row, any idea where I went wrong?

1
  • 1
    Have you tried debugging Courses Method? I don't think it's passing the id to the method. Try @Html.ActionLink("Grade", "Courses", "Students", new { @class = "btn btn-success" , id = CourseId})
    – Gogo Dev
    Commented Jun 27 at 6:51

0

Browse other questions tagged or ask your own question.