无法在后面的代码中访问数据列表控件ID

时间:2019-04-04 10:09:02

标签: c# html asp.net-mvc controls datalist

我正在尝试将字符串列表绑定到数据列表。我正在尝试在控制器中进行绑定,并且数据列表在视图(chtml文件)中。我不断收到错误消息:“名称“数据列表ID”在当前上下文中不存在”。

您知道如何解决此问题吗?

<input list="cardProgram" class="form-control input-group-lg">
<datalist id="cardProgram" runat="server" />


protected void Page_Load(object sender, EventArgs e)
{
    BindCardPrograms(sender, e);
}

private async void BindCardPrograms(object sender, EventArgs e)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://localhost:59066/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new 
            MediaTypeWithQualityHeaderValue("application/json"));
        ViewBag.country = "";
        HttpResponseMessage response = await 
            client.GetAsync("api/Profile/InitializeCardProgramSelection");
        if (response.IsSuccessStatusCode)
        {
            List<String> cardPrograms = 
                response.Content.ReadAsAsync<List<String>>().Result;
            var builder = new System.Text.StringBuilder();
            foreach (String filename in cardPrograms)
            {
                builder.Append(String.Format("<option value='{0}'>", 
                filename));
                cardProgram.InnerHtml = builder.ToString();
            }
        }
        /*else
        {
            return View();
         }*/

    }
}

0 个答案:

没有答案