17
1
using System;
2
using System.ComponentModel.DataAnnotations;
3
4
namespace HelloWorldMvcApp
5
{
6
public class SampleViewModel
7
{
8
[Required]
9
[MinLength(10)]
10
[MaxLength(100)]
11
[Display(Name = "Enter your name:")]
12
public string Question { get; set; }
13
14
//See here for list of answers
15
public string Answer { get; set; }
16
}
17
}
102
1
@model HelloWorldMvcApp.SampleViewModel
2
@{
3
Layout = null;
4
}
5
6
<!DOCTYPE html>
7
<!-- template from http://getbootstrap.com/getting-started -->
8
9
<html lang="en">
10
<head>
11
<meta charset="utf-8">
12
<meta http-equiv="X-UA-Compatible" content="IE=edge">
13
<meta name="viewport" content="width=device-width, initial-scale=1">
14
<title>Bootstrap 101 Template</title>
15
16
<!-- CSS Includes -->
17
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
18
19
<style type="text/css">
20
21
.field-validation-error {
22
color: #ff0000;
23
}
24
28
1
using System;
2
using System.Web.Mvc;
3
using System.Collections.Generic;
4
5
namespace HelloWorldMvcApp
6
{
7
public class HomeController : Controller
8
{
9
[HttpGet]
10
public ActionResult Index()
11
{
12
return View(new SampleViewModel());
13
}
14
15
16
[HttpPost]
17
public JsonResult GetAnswer(string question)
18
{
19
int index = _rnd.Next(_db.Count);
20
var answer = _db[index];
21
return Json(answer);
22
}
23
24
private static Random _rnd = new Random();
25
26
private static List<string> _db = new List<string> { "Yes", "No", "Definitely, yes", "I don't know", "Looks like, yes"} ;
27
}
28
}