簡體   English   中英

無法使用數據庫中的值填充編輯復選框或下拉列表

[英]Trouble populating edit checkbox or dropdown list with value from database

情境

我有一個問題,該問題利用了jquery轉發器,因此我可以在這種情況下繼續添加記錄。

問題

我遇到的問題是我無法使用“是/否”下拉列表或復選框來工作或顯示它從數據庫讀取的值。 如您所見,從屏幕截圖中,我無法選擇要在下拉列表中顯示的值“ no”,如果選擇了true,則無法選中我的復選框

代碼的注釋區域顯示了我使用的一些方法,如您所見,我無法完全綁定行為(如果我輸入錯誤,請在此處更正),因為它是另一個類的一部分,所以我不能說“ Model.historyLead”或Model .act.history領導。 我將返回所有其他值,但只是復選框或下拉列表對我不起作用。 對於解決此問題的任何幫助,我將深表感謝。 問候

在此處輸入圖片說明

我的編輯視圖的一部分

      @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>

和模型使用

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; }
    }
}

當您的條件為true時,嘗試添加一個選中的屬性:

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

你也可以一個布爾屬性添加到您的視圖模型,然后使用CheckBoxFor如圖所示這里

您誤解了復選框的工作方式。

  1. 選中復選框的Value會發送到服務器
  2. 如果未選中此復選框,則沒有Value發送到服務器

使用這兩個規則,您可以說Value應該為Yes因此如果選中,它將Yes的值發回服務器。 如果未選中,則可以if (String.IsNullOrEmpty(historyLead)) historyLead = "No"添加一些邏輯。

但是我建議將屬性historyLead更改為Boolean並將checkbox的值設置為True 由於默認值,如果未選中,它將變為False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM