Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

c# - Checkbox always show False either it is True

I am try to understand what happened with checkbox status. So I have Create Page where I insert Patient and when I click Emergency to True in Index Page I always get False

IndexPage.cshtml

  @if (Model.Count() > 0)
    {
        <table class="table table-bordered table-striped" style="width:100%">
            <thead>
                <tr>
                    <th>
                        Doctor Full Name - CODE
                    </th>

                    <th>
                        Patient Full Name
                    </th>
                    <th>
                        Date and Time
                    </th>
                    <th>
                        Emergency
                    </th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var obj in Model)
                {
                    <tr>
                        <td width="25%">@obj.Doctor.Firstname @obj.Doctor.Lastname @obj.Doctor.Code</td>
                        <td width="25%">@obj.Patient.FirstName @obj.Patient.LastName</td>
                        <td width="25%">@obj.DateAndTime</td>
                        <td width="25%" class="blink_me">@obj.Emergency</td>



                        <td class="text-center">
                            <div class="w-75 btn-group" role="group">
                                <a asp-route-Id="@obj.Id" asp-action="Upsert" class="btn btn-primary mx-2">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <a asp-route-Id="@obj.Id" asp-action="Delete" class="btn btn-danger mx-2">
                                    <i class="far fa-trash-alt"></i>
                                </a>
                            </div>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    }
    else
    {
        <p> No Admission Patient exists.</p>
    }

Create

    @model BergClinics.Models.ViewModel.AdmisionVM
@{
    ViewData["Title"] = "Upsert";
    var title = "Create Admission Patient";
}

<form method="post" enctype="multipart/form-data">
    @if (Model.AdmissionPatient.Id != 0)
    {
        <input asp-for="AdmissionPatient.Id" hidden />
        title = "Edit Admission Patient";
    }
    <div class="border p-3">
        <div class="form-group row">
            <h2 class="text-info pl-3">@title</h2>
        </div>

        <div class="row">
            <div class="col-8">
                <div class="form-group row py-2">
                    <div class="col-4">
                        <label>Doctor Full Name : </label>
                    </div>
                    <div class="col-8">
                        <select asp-for="AdmissionPatient.DoctorId" asp-items="@Model.DoctorSelectList" class="form-control">
                            <option disabled selected>--Select Docotor--</option>
                        </select>
                    </div>
                </div>

                <div class="form-group row py-2">
                    <div class="col-4">
                        <label>Patient Full Name: </label>
                    </div>
                    <div class="col-8">
                        <select asp-for="AdmissionPatient.PatientId" asp-items="@Model.PatientSelectList" class="form-control">
                            <option disabled selected>--Select Patient--</option>
                        </select>
                    </div>

                </div>


                <div class="form-group row py-2">
                    <div class="col-4">
                        <label>Date and Time :</label>
                    </div>
                    <div class="col-8">
                        <input asp-for="AdmissionPatient.DateAndTime" asp-format="{0:dd/MM/yyyy}" type="text" name="date" class="form-control datepicker" autocomplete="off">
                    </div>
                </div>

                <div class="form-group row py-2">
                    <div class="col-4">
                        <label>Patient Image :</label>
                    </div>
                    <div class="col-3">
                        <input type="file" name="files" id="imageBox" multiple class="form-control" />

                    </div>
                </div>


                <div class="form-group row py-2">
                    <div class="col-4">
                        <label>Emergency reception :</label>
                    </div>
                    <div class="col-8">
                        <input type="checkbox" class="form-control" id="emergencyId">
                        <label class="form-check-label" for="exampleCheck1"></label>
                    </div>
                </div>



                <div class="form-group row py-2">
                    <div class="col-8 offset-4 row">

                        <div class="col">
                            @if (Model.AdmissionPatient.Id != 0)
                            {
                                //update
                                <input type="submit" class="btn btn-info w-100" value="Update" />
                            }
                            else
                            {
                                //create
                                <input type="submit" onclick="return validateInput()" class="btn btn-primary w-100" value="Create" />
                            }

                        </div>
                    </div>
                </div>
            </div>
            <div class="col-4">
                @if (Model.AdmissionPatient.Id != 0)
                {
                    <img src="@[email protected]" width="100%" style="border-radius:5px; border:1px solid #bbb" />
                }
            </div>
        </div>
    </div>

</form>



@section Scripts{

    @{
        <partial name="_ValidationScriptsPartial" />
    }
    <script>

        function validateInput() {
            if (document.getElementById("imageBox").value == "") {
                Swal.fire(
                    'Error!',
                    'Please upload an Image!',
                    'error'
                )
                return false;
            }
            return true;



        }

    </script>

    <script>
        $('.datepicker').datepicker({
            startDate: new Date()
        });
    </script>
}

Controller Post Action

[HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Upsert(AdmisionVM admissionVM)
        {

            if (ModelState.IsValid)
            {
                var files = HttpContext.Request.Form.Files;
                string webRootPath = _webHostEnvironment.WebRootPath;



                if (admissionVM.AdmissionPatient.Id == 0)
                {
                    //Creating
                    string upload = webRootPath + Constans.imagePath;
                    string fileName = Guid.NewGuid().ToString();
                    string extension = Path.GetExtension(files[0].FileName);

                    using (var fileStream = new FileStream(Path.Combine(upload, fileName + extension), FileMode.Create))
                    {
                        files[0].CopyTo(fileStream);
                    }

                    admissionVM.AdmissionPatient.Image = fileName + extension;
                    _db.AdmissionPacients.Add(admissionVM.AdmissionPatient);
                }
                else
                {
                    //Updating
                    var objFromDb = _db.AdmissionPacients.AsNoTracking().FirstOrDefault(u => u.Id == admissionVM.AdmissionPatient.Id);

                    if (files.Count > 0)
                    {
                        string upload = webRootPath + Constans.imagePath;
                        string fileName = Guid.NewGuid().ToString();
                        string extension = Path.GetExtension(files[0].FileName);

                        var oldFile = Path.Combine(upload, objFromDb.Image);

                        if (System.IO.File.Exists(oldFile))
                        {
                            System.IO.File.Delete(oldFile);
                        }

                        using (var fileStream = new FileStream(Path.Combine(upload, fileName + extension), FileMode.Create))
                        {
                            files[0].CopyTo(fileStream);
                        }

                        admissionVM.AdmissionPatient.Image = fileName + extension;
                    }
                    else
                    {
                        admissionVM.AdmissionPatient.Image = objFromDb.Image;
                    }

                    _db.AdmissionPacients.Update(admissionVM.AdmissionPatient);
                }

                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            admissionVM.PatientSelectList = _db.Patients.Select(i => new SelectListItem
            {
                Text = i.FirstName + i.LastName,
                Value = i.Id.ToString()
            });

            admissionVM.DoctorSelectList = _db.Doctors.Select(i => new SelectListItem
            {
                Text = i.Firstname + i.Lastname,
                Value = i.Id.ToString()
            });

            return View(admissionVM);
        }
question from:https://stackoverflow.com/questions/65843323/checkbox-always-show-false-either-it-is-true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try add name to this line:

<input type="checkbox" class="form-control" id="emergencyId">

to

 <input name="Emergency" type="checkbox" class="form-control" id="emergencyId">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...