html随机读取数据,初学者.cshtml if,随机数并将数据传回

I'm a C programmer trying to learn C# ASP.NET MVC using Visual Studio 2015 Community Edition.

So, I have my view, and I would like to randomly show one of two 'select something' questions:

I like to eat

@Html.DropDownList("answers[0]" + Model[0].ToSelectList(), "")

.

or

My hair is

@Html.DropDownList("answers[1]" + Model[1].ToSelectList(), "")

.

But it turns out I can't figure out either of the requirements:

generate a 50/50 random number

us it in an if to write HTML). Could anyone give me a shove in the right direction?

Also, I seem to be able to use this 'answers' structure without ever making it. If I end up with an 'answers' that only has data in say indexes 2, 9 and 33, does a 34 element array get passed back (Posted?)

Additional info I couldn't squeeze into comments:

@Christos I didn't want to overload the question with info, but I was thinking I would have a list of say 10 questions, but I only want each visitor to my page to answer 5. So I do the 'show one of these two questions' thing for five pairs, then I'll have my Controller do this:

[HttpPost]

public ActionResult Index(string[] answers)

{

StringBuilder sb = new StringBuilder();

foreach(var response in answers)

{

sb.Append(response);

sb.Append(",");

string responses = sb.ToString();

}

string time = DateTime.Now.ToString();

string output = time + "," + HttpResponseSubstitutionCallback;

StreamWriter sw = new StreamWriter("C:\\Temp\\responses.csv");

sw.WriteLine(output);

sw.Close();

return View();

}

When I pick up SQL I'll be able to improve this approach with databases, but for now I'm just so comfortable with CSV data it's far quicker and easier for me.

OptionModel as requested (note: I just 'borrowed' this from someone with more experience just before heading home yesterday)

public class OptionModel

{

public string SelectedOption { get; set; }

public List PossibleOptions { get; set; }

public OptionModel(params string[] possibleOptions)

{

PossibleOptions = possibleOptions.ToList();

}

public IEnumerable ToSelectList()

{

return PossibleOptions.Select(x => new SelectListItem { Text = x, Value = x });

}

}

Talk1:

The way you are naming the means that you would not be able to bind to a model when you submit. Can you show the model, including the .ToSelectList() method.

Talk2:

question updated.

Talk3:

Based on your original question (show 1 of 2 dropdownlists), then the answer that Christos has given would work if you changed it to @Html.DropDownList("answers", ...) (no indexer) because your parameter is string[] answers - although it could also be string answer. However none of this would really do anything useful. The parameter might contain "Apples" but you don't even know what the question was.

Talk4:

So I need some way for answers[n] to contain the response to Model[n], and for it not to contract up on itself. Could I just start by making an answers[] and filling it with the word NULL based on Model.Length()?

Talk5:

And just for fun, have a look at this DotNetFiddle - click run repeatedly

Solutions1

You could try something like this:

@{

Random rnd = new Random();

// This will return either 1 or 2 randomly.

int question = rnd.Next(1, 3);

}

@if(question==1)

{

I like to eat

@Html.DropDownList("answers" + Model[0].ToSelectList(), "")

}

else

{

My hair is

@Html.DropDownList("answers" + Model[1].ToSelectList(), "")

}

When we make use of a block starting with the @, @{ }, we can place any valid c# code in this block, like to declare variables, methods etc. and later we can make use of them.

Talk1:

Good answer. Additionally @icantdopretty can research razor C#, here is a solid starting point.

Talk2:

This would generate non-consecutive indexers and would not bind to a model when posting.

Talk3:

I get your point. But with the posted code and question this came out. If it's wrong, please let me known how to correct it or to delete my answer. Thank you very much in advance.

Talk4:

OP has not given us enough info yet to determine what would be the best way to solve issues with their design.

Talk5:

Thanks Christos. Why wouldn't it be rnd.Next(1,2) to go 50/50?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值