为什么我得到" System.Web.Mvc.SelectListItem"在我的DropDownList中?

时间:2016-03-02 18:59:57

标签: c# asp.net asp.net-mvc html-select

我相信我已正确绑定了我的数据,但我似乎无法为每个SelectListItem获取正确显示的text属性。

我的模特:

public class Licenses
    {
        public SelectList LicenseNames { get; set; }
        public string SelectedLicenseName { get; set; }
    }

控制器:

[HttpGet]
    public ActionResult License()
    {
        try
        {
            DataTable LicsTable = BW.SQLServer.Table("GetLicenses", ConfigurationManager.ConnectionStrings["ProfressionalActivitiesConnection"].ToString());
            ProfessionalActivities.Models.Licenses model = new ProfessionalActivities.Models.Licenses();
            model.LicenseNames = new SelectList(LicsTable.AsEnumerable().Select(row =>
            new SelectListItem
            {
                Value = row["Description"].ToString(),
                Text = "test"
            }));
            return PartialView("_AddLicense", model);
        }
        catch (Exception ex)
        {
            var t = ex;
            return PartialView("_AddLicense");
        }
    }

查看:

@Html.DropDownList("LicenseNames", new SelectList(Model.LicenseNames, "Value", "Text", Model.LicenseNames.SelectedValue), new { htmlAttributes = new { @class = "form-control focusMe" } })

2 个答案:

答案 0 :(得分:9)

使用Items属性的LicenseNames属性SelectList

@Html.DropDownList("SelectedLicenseName", new SelectList(Model.LicenseNames.Items,
                                       "Value", "Text", Model.LicenseNames.SelectedValue))

或使用DropDownListFor辅助方法

@Html.DropDownListFor(d=>d.SelectedLicenseName, 
                                         Model.LicenseNames.Items as List<SelectListItem>)

因此,当您发布表单时,您可以检查SelectedLicenseName属性

[HttpPost]
public ActionResult Create(Licenses model)
{
  //check model.SelectedLicenseName
}  

答案 1 :(得分:0)

我明确设置了public void registerToken(NodeRef nodeRef) throws IdenticalContentException { final AtomicBoolean stillRunning = new AtomicBoolean(true); String lockToken = null; String nodeRefToken = getToken(nodeRef); try { lockToken = this.jobLockService.getLock(getLock(nodeRefToken), 30000L, 3000L, 2); JobLockService.JobLockRefreshCallback callback = new JobLockService.JobLockRefreshCallback() { public void lockReleased() { stillRunning.set(false); } public boolean isActive() { return stillRunning.get(); } }; this.jobLockService.refreshLock(lockToken, getLock(nodeRefToken), 30000L, callback); if (stillRunning.get()) { registerNode(nodeRef, nodeRefToken); } } catch (LockAcquisitionException lae) { LOG.error("Failed to acquire a lock for metadata checking", lae); throw lae; } finally { stillRunning.set(false); if (lockToken != null) { this.jobLockService.releaseLock(lockToken, getLock(nodeRefToken)); } } } dataValueField名称。

dataTextField

然后,您无需在观看中写new SelectListItem { Value = row["Description"].ToString(), Text = "test" }), "Value", "Text"); (如您接受的答案所示)。