简体   繁体   English

无法使用数据库中的值填充编辑复选框或下拉列表

[英]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. 我有一个问题,该问题利用了jquery转发器,因此我可以在这种情况下继续添加记录。

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" 如您所见,从屏幕截图中,我无法选择要在下拉列表中显示的值“ no”,如果选择了true,则无法选中我的复选框

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. 代码的注释区域显示了我使用的一些方法,如您所见,我无法完全绑定行为(如果我输入错误,请在此处更正),因为它是另一个类的一部分,所以我不能说“ Model.historyLead”或Model .act.history领导。 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: 当您的条件为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 你也可以一个布尔属性添加到您的视图模型,然后使用CheckBoxFor如图所示这里

You misunderstand how checkbox works. 您误解了复选框的工作方式。

  1. Value of checkbox is sent to server when it is checked 选中复选框的Value会发送到服务器
  2. If checkbox is not checked no Value is sent to server 如果未选中此复选框,则没有Value发送到服务器

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. 使用这两个规则,您可以说Value应该为Yes因此如果选中,它将Yes的值发回服务器。 If unchecked you can add some logic if (String.IsNullOrEmpty(historyLead)) historyLead = "No" . 如果未选中,则可以if (String.IsNullOrEmpty(historyLead)) historyLead = "No"添加一些逻辑。

But i would suggest changing property historyLead to Boolean and setting value of checkbox to True . 但是我建议将属性historyLead更改为Boolean并将checkbox的值设置为True Because of default value it would become False in case if it was unchecked. 由于默认值,如果未选中,它将变为False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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