簡體   English   中英

檢查表包含字符串

[英]checking list contains string

有一個將用於遠程驗證功能的類,但無法使其正常工作

[HttpPost]
public JsonResult doesUserNameExist(string Forename)
{
    IEnumerable<SelectListItem> user = new List<SelectListItem>();
    using (EIPInternalEntities ctx = new EIPInternalEntities())
    {
        user = new SelectList(ctx.Database
                                 .SqlQuery<string>("EXEC dbo.uspGetLkUpJobTitle")
                                 .ToList());
    }

    var userlist = user.ToList();

    //return Json(user == null);
    return Json(!userlist.Contains(Forename));
}

嘗試了不同的方法,但當前(Forename))被標記為錯誤

“參數1無法從'字符串'轉換為'System.Web.Mvc.SelectListItem'

如果我嘗試

var userlist = (SelectList)user;

//return Json(user == null);
return Json(!userlist.Contains(Forename));

然后!userList 被警告說

SelectList不包含包含的定義

嘗試這個

return Json(!userlist.Any(x => x.Text == Forename));

您可以嘗試以下方法:

!userlist.Any(item => item.Text == Forename);

userlist是列表SelectListItem對象。 每個SelectListItem具有三個屬性, NameValueSelected 我認為您顯然想查找Text 如果要查找值,只需使用item.Value重寫以上item.Value

暫無
暫無

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

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