0
votes

I am working on the following. When I run the code, I am able to get the data in the correct form from the database via GET method and I am able to display it correctly in the View. However, on editing/changing some data, say Type, and on pressing Save Data, the POST method gets called, but the studentsViewModel value as the parameter comes as null. No idea why.

I have gone through similar questions on the website and other places online. But they are not having what I am exactly looking for. Plus, most of them are using HTMLHelpers.

Here are the code snippets:

Controller

public IActionResult StudentsDetails(int Id)
{
    try
    {
        var typeItems = _context.Type.Where(t => t.IsActive == true).Select(us => new SelectListItem { Value = us.TypeId.ToString(), Text = us.TypeName.ToString() }).ToList();
        ViewBag.TypeItems = typeItems;

        var studentsData = _context.Students
            .Where(r => r.StudentId == Id).ToList();

        var studentsViewModelData = new tudentsViewModel();

        studentsViewModelData.Students = studentsData;
        studentsViewModelData.StudentId = Id;

        return View(studentsViewModelData);
    }
    catch (Exception ex)
    {
        ....      
    }

    ....
}

[HttpPost]
public IActionResult StudentsDetails(int Id, StudentsViewModel studentsViewModel)
{
    try
    { 
        if(ModelState.IsValid)
        {
            ...
            studentsViewModel.StudentId = Id;
            ....     
        }
        catch(Exception ex)
        {
            ....
        }

        ....
    }
}

Details.cshtml

<div class="card-body card-padding">
    <form method="post" asp-action="Details" asp-controller="Students" id="studentsForm">
        ....
        <table id="data-table-basic" class="table table-striped table-vmiddle">                    
            @if(Model.Students.Count() != 0)
            {
                <thead>
                    <tr>
                        <th data-column-id="StudentNumber">Student Number</th>
                        <th data-column-id="Type">Student Type</th>
                    </tr>
                </thead>
            }
            <tbody>
                @for(int count = 0;count < Model.Students.Count; count++)
                {
                    <tr>
                        <td>                                           
                            @Model.Students[count].StudentNumber
                        </td>
                        <td>
                            <select asp-for="@Model.Students[count].TypeId" asp-items="@ViewBag.TypeItems" class="chosen" data-placeholder="Select student type" name="selectType" id="selectType">
                                <option value=""></option>
                            </select>        
                        </td>
                    </tr>                                          
                }
            </tbody>                       
        </table>
        <div>
            <button type="submit" value="Save">Save Data</button>
        </div>
    </form>
</div>

Model

public class Students
{
    [Key]
    public int StudentId { get; set; }

    public string StudentNumber { get; set; }

    [ForeignKey("Type")]
    public int TypeId { get; set; }
    public virtual Type Type { get; set; }

}

StudentsViewModel.cs

public class StudentsViewModel
{
    public int StudentId { get; set; }

    public List<Students> Students { get; set; }  
}

EDIT: Adding Type class

Type.cs

public class Type
{
    [Key]
    public int TypeId { get; set; }

    public string TypeName { get; set; }
    public bool IsActive { get; set; }
}
1
Could you add [FromBody] attribute in parameter and try again? public IActionResult StudentsDetails(int Id, [FromBody] StudentsViewModel studentsViewModel) - Win
@Win On running the code after making the changes, as soon as I press Submit Data, the page says: This page isn't working` and gives me a HTTP ERROR 415. - Dashamlav
@Dashamlav 415 is unsupported media type -- what is being sent up the wire? Can you look in the network tab of Dev Tools in the browser and post what's there? It feels like more of the page markup is needed to replicate your issue and provide an answer. - Stand__Sure
@Dashamlav I notice that action method name doesn't match asp-action="Details" vs asp-action="StudentsDetails". Is this a typo in your question? Otherwise, you could not even reach to the action method. Could you show us StudentsTable class? - Win
@Win That's a typo! - Dashamlav

1 Answers

0
votes

You should not set id and name to select explicitly. Remove name="selectType" id="selectType".

<select asp-for="@Model.Students[count].TypeId" 
        asp-items="@ViewBag.TypeItems"
        class="chosen" 
        data-placeholder="Select student type">
    <option value=""></option>
</select>