[英]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
如图所示这里
您误解了复选框的工作方式。
Value
会发送到服务器 Value
发送到服务器 使用这两个规则,您可以说Value
应该为Yes
因此如果选中,它将Yes
的值发回服务器。 如果未选中,则可以if (String.IsNullOrEmpty(historyLead)) historyLead = "No"
添加一些逻辑。
但是我建议将属性historyLead
更改为Boolean
并将checkbox的值设置为True
。 由于默认值,如果未选中,它将变为False
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.