简体   繁体   中英

Trouble populating edit checkbox or dropdown list with value from database

Scenario

I have a problem that utilizes a jquery repeater that way I can keep adding records in this case qualification records.

Problem

The problem I am experiencing is that I cannot get a Yes/No dropdown or checkbox to work or display the value that it reads from the database. As you can see,from the screenshot I can't select the value "no" to be displayed in my dropdown list, if true was selected, I wouldnt be able to get my checkbox to be "ticked"

The commented area of code shows some of the methods I've used, As you can see, I cant exactly bind act (Correct me here if im wrong) because it's part of another class so I cant say "Model.historyLead" or Model.act.historyLead. I am returning all the other values but it's just the checkbox or dropdown list is not working for me. I would greatly appreciate any help in solving this. Regards

在此处输入图片说明

Part of my edit view

      @foreach (var item in Model.act)
 {
    <div data-repeater-item class="mt-repeater-item">
        <div class="mt-repeater-input">
                <label>Institution</label>
                    <input type="text" class="form-control" name="hisotryInput" value="@item.hisotryInput" />
        </div>

               <div class="mt-repeater-input">
                    <label>Description</label>                                             
                            <div class="col-md-10">                                                           
                                @Html.TextArea("hisotrydescrip",item.hisotrydescrip, new { @class = "form-control", cols = 50, @rows = 5,@style="width:290px" })                                                             
                            </div>
                </div>

                <div class="mt-repeater-input">
                    <label>Lead Consultant?</label>
                        @*@Html.DropDownList("historyLead", null, String.Empty, new { @class = "form-control ", @Value = @item.historyLead })*@
                        @*@Html.CheckBox("historyLead", new { htmlAttributes = new { @class = "form-control",@value=@item.historyLead } })*@
                        <input type="checkbox" class="form-control" name="historyLead" value=@item.historyLead />
                                @*<select name="historyLead" >
                                    <option value="volvo">Volvo</option>
                                    <option value="saab">Saab</option>
                                    <option value="vw">VW</option>
                                    <option value="audi" selected>Audi</option>
                                </select>*@
                    </div>

And model used

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace CARICAD_App.Models
{
    public class repeatViewModel
    {
        [Display(Name = "textinput")]
        public string textinput { get; set; }





    }

    public class CON_consultantInfoVM
    {

        [DisplayName("Age Range")]
        public Nullable<int> ageRange { get; set; }
        public int id { get; set; }
        [DisplayName("Image")]
        public string image { get; set; }
        [DisplayName("Consultant ID")]
        public string consultID { get; set; }
        [DisplayName("First Name")]
        [Required]
        public string firstName { get; set; }

...
        public REF_country REF_country { get; set; }
        public REF_nationality REF_nationality { get; set; }

        public List<actVm> act { get; set; }
    }

    public class actVm
    {
        public Nullable<int> id { get; set; }
        public string consultID { get; set; }
        public string textinput { get; set; }
        public string untypedinput { get; set; }

        public string dateinput { get; set; }
        public string textareainput { get; set; }
        public string radioinput { get; set; }

        public string selectinput { get; set; }
        public string activities { get; set; }
        public string hisotryInput { get; set; }
        public string historydateinput { get; set; }
        public string hisotrydescrip { get; set; }
        public string historydateinputTo { get; set; }
        public string historyLead { get; set; }
        public bool historyCo { get; set; }
        public List<string> multipleselectinput { get; set; }
    }
}

Try adding a checked attribute when your condition is true:

<input type="checkbox" name="historyLead" value="@item.historyLead" @(item.historyLead== "Yes" ? "checked='checked'" : "") class="form-control" />

You could also add a boolean property to your viewmodel and then use CheckBoxFor as shown here

You misunderstand how checkbox works.

  1. Value of checkbox is sent to server when it is checked
  2. If checkbox is not checked no Value is sent to server

With this two rules you can say that Value should be Yes so it will post back the value of Yes to server if checked. If unchecked you can add some logic if (String.IsNullOrEmpty(historyLead)) historyLead = "No" .

But i would suggest changing property historyLead to Boolean and setting value of checkbox to True . Because of default value it would become False in case if it was unchecked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM