我想在下拉列表中获取所选值。
我的观点:
procedure TForm1.DBNavigator1BeforeAction(Sender: TObject; Button:
TNavigateBtn);
var
Res : Integer;
DataSet : TDataSet;
begin
DataSet := DBNavigator1.DataSource.DataSet;
case Button of
nbFirst,
nbPrior,
nbNext,
nbLast: begin
if DataSet.State in [dsEdit, dsInsert] then begin
Res := MessageDlg('The current row has unsaved changes. Abandon them?', mtWarning, [mbYes, mbNo], 0);
if Res = mrYes then
DataSet.Cancel
else
DataSet.Post;
end;
end;
end;
end;
而且,我的控制器:
<div class="form-group">
@Html.LabelFor(model => model.AccountRefNo, "Account Desc", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("AccountRefNo", ViewBag.AccountRefNo as SelectList, "Account Desc", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.AccountRefNo)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PartyID, "Party Type", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("TypeID", ViewBag.TypeID as SelectList, "Party Type", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TypeID)
</div>
</div>
似乎没关系,但我不知道问题出在哪里。两个下拉列表都填写了列表,但两者都选择了他们的OptionLabel,即分别选择“Party Type”和“Account Desc”。
请帮忙。谢谢!
答案 0 :(得分:0)
SelectList构造函数&#39;第一个参数需要采用SelectListItem的集合:
尝试以下操作,只要有数据,它就应该有效(在PartyTypes和ChartOfAccounts上为null引用:
ViewBag.TypeID = new SelectList
(
db.PartyTypes.Select(m=>new SelectListItem {Text=m.TypeDesc, Value=m.TypeID})
, "TypeID"
, "TypeDesc"
, party.TypeID
);
ViewBag.AccountRefNo = new SelectList
(
db.ChartOfAccounts
.Where(a => codes.Contains (a.AccountCode))
.Select(m=>new SelectListItem({Text=m.AccountDesc, Value=m.AccountCode}))
, "AccountCode"
, "AccountDesc"
, party.AccountRefNo //this maybe wrong
);
此外,如果ViewBag.AccountRefNo和party.AccountRefNo是同一类型,则第二个SelectList构造函数的最后一个参数必须不正确。参数应该是选定的值,所以很可能是party.AccountCode? p>
答案 1 :(得分:0)
这是在编辑控制器中,然后工作。
string typeDescSelected = (from sub in db.PartyTypes
where sub.TypeID == party.TypeID
select sub.TypeDesc).First();
ViewBag.TypeDesc = new SelectList(db.PartyTypes, "TypeID", "TypeDesc", typeDescSelected);
string[] codes = { "2.20.40.10", "3.50.10.20" };
string accountDescSelected = (from sub in db.ChartOfAccounts
where sub.AccountCode == party.AccountRefNo
select sub.AccountDesc).First();
ViewBag.AccountDesc = new SelectList(db.ChartOfAccounts, "AccountCode", "AccountDesc", accountDescSelected);
return View(party);