jeudi 7 décembre 2017

How to post checkbox value in ASP.NET Core MVC [duplicate]

This question already has an answer here:

I'm trying to post some Input data with some checkbox in ASP.NET Core MVC App but I'm not getting the checked checkbox data in my Controller only getting text input values. I'm new to ASP.NET and I really don't know how to solve this problem.

Here is my .cshtml

@model SchoolViewModel
<form asp-action="AddEdit" method="post" id="frm">
<div class="col-md-4">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <div class="form-group">
        <label asp-for="@Model.Student.LastName" class="control-label"></label>
        <input asp-for="@Model.Student.LastName" class="form-control" />
        <span asp-validation-for="@Model.Student.LastName" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="@Model.Student.FirstMidName" class="control-label"></label>
        <input asp-for="@Model.Student.FirstMidName" class="form-control" />
        <span asp-validation-for="@Model.Student.FirstMidName" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="@Model.Student.EnrollmentDate" class="control-label"></label>
        <input asp-for="@Model.Student.EnrollmentDate" class="form-control" id="datetime" />
        <span asp-validation-for="@Model.Student.EnrollmentDate" class="text-danger"></span>
    </div>
    <div class="form-group">
        <input type="submit" value="Save" class="btn btn-default" />
    </div>
</div>

<div class="col-md-8">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>
                    Course Name
                </th>
                <th>
                    Credits
                </th>
                <th>
                    Course Id
                </th>
            </tr>
        </thead>
        <tbody> 
            @foreach (var course in Model.Courses)
            { 
                <tr>
                    <td>
                        <div class="form-group">
                            <input asp-for="@course.IsChecked" type="checkbox" class="checkbox-inline"/>  
                            <label asp-for="@course.Title" class="checkbox-inline">@course.Title</label>
                            <input asp-for="@course.CourseID" type="hidden" />
                            <input asp-for="@course.Title" type="hidden" /> 
                        </div>
                    </td>
                    <td>
                        @course.Credits
                    </td>
                    <td>
                        @course.CourseID
                    </td>
                </tr>
            } 
        </tbody>
    </table> 
</div>

Here is my Controller.cs

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> AddEdit(SchoolViewModel model) 
    { 

        try
        {
            if (ModelState.IsValid)
            {
                _context.Add(model.Student);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
        }
        catch (DbUpdateException ex)
        { 
            ModelState.AddModelError("", "Unable to save changes. + ex);
        } 

        return View(model); 
    }

here I'm getting the Student properly but Courses is null enter image description here




Aucun commentaire:

Enregistrer un commentaire