string.contains(string) 匹配整个单词

ern*_*ay2 0 c# validation asp.net-mvc formcollection asp.net-mvc-4

在我看来,

@using(Html.BeginForm("Action", "Controller", FormMethod.Post)){
<div>
    @Html.TextBox("text_1", " ")
    @Html.TextBox("text_2", " ")
    @if(Session["UserRole"].ToString() == "Manager"){
    @Html.TextBox("anotherText_3", " ")
    }
</div>
<button type="submit">Submit</button>
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,

public ActionResult Action(FormCollection form){
    if(!form.AllKeys.Contains("anotherText")){
        ModelState.AddModelError("Error", "AnotherText is missing!");
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一个表单并发布到我的方法中,在我的方法中,我想检查 id 是否包含“anotherText”的文本框,但我使用 .Contains() 它总是给出 false,这在我的表单集合中找不到...如何我可以检查包含“anotherText”的 id 文本框是否存在吗?

Gra*_*ICA 5

搜索失败是有道理的,因为它不是完全匹配。

尝试使用StartsWith,看看是否有任何键以您要查找的值开头。

if (!form.AllKeys.Any(x => x.StartsWith("anotherText")))
{
    // add error
}
Run Code Online (Sandbox Code Playgroud)