Beginning. cshtml if, random numbers and pass data back

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

So, I have my opinion, and I would like to randomly display one of two "pick something" questions:

<p>I like to eat
@Html.DropDownList("answers[0]" + Model[0].ToSelectList(), "") 
.
</p>

      

or

<p>My hair is
@Html.DropDownList("answers[1]" + Model[1].ToSelectList(), "") 
.
</p>

      

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

  • generate random number 50/50
  • us this in if, if we write HTML). Can anyone give me a nudge in the right direction?

Also, I seem to be able to use this response structure without creating it. If I get "replies" responses that only have data at indices 2, 9 and 33, is an array of 34 elements returned (Added?)

Additional information I could not go into comments:

@Christos I didn't want to overload the question with information, but I thought I would have a list of 10 questions, but I want every visitor to my page to answer 5. So I make a show one of these two questions for five couples, then I I will make my controller like 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 can improve this approach with databases, but so far I am so comfortable with CSV data that it is much faster and easier for me.

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

public class OptionModel
{
    public string SelectedOption { get; set; }
    public List<string> PossibleOptions { get; set; }

    public OptionModel(params string[] possibleOptions)
    {
        PossibleOptions = possibleOptions.ToList();
    }

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

      

+3


source to share


1 answer


You can 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)
{
    <p>I like to eat
    @Html.DropDownList("answers" + Model[0].ToSelectList(), "") 
    </p>
}
else
{
    <p>My hair is
    @Html.DropDownList("answers" + Model[1].ToSelectList(), "") 
    </p>
}

      



When we use a block that starts with @

, @{ }

we can put any valid C # code in that block, like declaring variables, methods, etc., and then we can use them.

+2


source







All Articles