24
1
using System;
2
using System.ComponentModel.DataAnnotations;
3
4
namespace HelloWorldMvcApp
5
{
6
public class SampleViewModel
7
{
8
[Required]
9
[StringLength(100, MinimumLength = 3)]
10
[Display(Name = "Ask Magic 8 Ball Any Question:")]
11
public string Question
12
{
13
get;
14
set;
15
}
16
17
//See here for list of answers
18
public string Answer
19
{
20
get;
21
set;
22
}
23
}
24
}
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
30
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
private static Random _rnd = new Random();
10
private static List<string> _db = new List<string>
11
{
12
"When pigs fly", "Yep", "Prolly", "Ask me later", "The answer will cost you your soul", "Yes", "No", "Definitely, yes", "I don't know", "Looks like, yes", "Why would you ask such an ignert question"
13
}
14
15
;
16
[HttpGet]
17
public ActionResult Index()
18
{
19
return View(new SampleViewModel());
20
}
21
22
[HttpPost]
23
public JsonResult GetAnswer(string question)
24
{
25
int index = _rnd.Next(_db.Count);
26
var answer = _db[index];
27
return Json(answer);
28
}
29
}
30
}