当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Operand type conflicts (char-string)

问题描述:

I am currently trying to create a droplist for users to select a month on. The droplist in itself works, but whenever I try to get the value of it, it fails. I think the easiest way to explain it is to show my code and the error message, since I'm not sure how else I would go about it.

Error message:

CS0019: Operator '==' cannot be applied to operands of type 'char' and 'string'

HTML with the dropdown list:

<h4 class="monthYear">

<form action="WebForm1.aspx" method="get">

<select name="monthList">

<option class="month">Januari</option>

<option class="month">Februari</option>

<option class="month">Mars</option>

<option class="month">Maj</option>

<option class="month">Juli</option>

<option class="month">Juni</option>

<option class="month">Augusti</option>

<option class="month">Oktober</option>

<option class="month">September</option>

<option class="month">November</option>

<option class="month">December</option>

</select>

<input type="submit" name="setMonth" value="Visa" />

</form>

</h4>

C# code where I try to get whatever they selected from the list:

<%

var Januari = Request["Januari"];

var Februari = Request["Februari"];

var monthList = Request["monthList"];

foreach (var month in monthList)

{

if (month == "Januari") <-- This == is what it doesn't seem to like.

{

Response.Write("Working");

var setYear = 2015;

var setMonth = 1;

}

}

%>

网友答案:

monthList is a string, and by doing an foreach on it, you get chars.

I guess you want to split the monthList on something, a ; maybe:

foreach (var month in monthList.Split(';'))
{ }

Now, month is a string.

If the returned value isn't a list of anything, you simply might want to drop the foreach at all.

分享给朋友:
您可能感兴趣的文章:
随机阅读: