Mainpage.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for Mainpage
/// </summary>
///
namespace Models
{
public class Mainpage
{
private SqlConnection sqlConn;
private string connStr;
public Mainpage()
{
//
// TODO: Add constructor logic here
//
}
private void sqlInit()
{
connStr = ConfigurationManager.ConnectionStrings["TestInput"].ConnectionString;
sqlConn = new SqlConnection(connStr);
}
public Mainpage(int id, string ld, DateTime? dm, string op1, string op2)
{
ID = id;
LineDesc = ld;
DateMade = dm;
Options1 = op1;
Options2 = op2;
}
public int ID { get; set; }
public string LineDesc { get; set; }
public DateTime? DateMade { get; set; }
public string Options1 { get; set; }
public string Options2 { get; set; }
public void Save()
{
sqlInit();
string insertStr = "usr_InsertValues";
SqlCommand sqlCmd = new SqlCommand(insertStr, sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
SqlParameter sp;
if (DateMade.HasValue)
sp = new SqlParameter("@p2", DateMade.Value);
else
sp = new SqlParameter("@p2", DBNull.Value);
SqlParameter[] sps = new SqlParameter[] {new SqlParameter("@p1", LineDesc),
sp,
new SqlParameter("@p3", Options1),
new SqlParameter("@p4", Options2)};
sqlCmd.Parameters.AddRange(sps);
sqlConn.Open();
sqlCmd.ExecuteNonQuery();
sqlConn.Close();
}
}
}
Default.cs (ViewModel)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Default
/// </summary>
///
namespace ViewModels
{
public class Default
{
public Default()
{
//
// TODO: Add constructor logic here
//
}
public Default(string desc, string sop1, string sop2, string sop3)
{
Description = desc;
SelectedOp1 = sop1;
SelectedOp2 = sop2;
SelectedOp3 = sop3;
}
public string Description { get; set; }
public string SelectedOp1 { get; set; }
public string SelectedOp2 { get; set; }
public string SelectedOp3 { get; set; }
public List<string> Option1 { get; set; }
public List<string> Option2 { get; set; }
public List<string> Option3 { get; set; }
public void Save()
{
Models.Mainpage mp = new Models.Mainpage(0, Description, new DateTime?(DateTime.Now), SelectedOp1, SelectedOp2);
mp.Save();
}
}
}
Default.cshtml (Controller/Code behind)
@{
var linedesc = Request.Form["linedesc"];
var datemade = DateTime.Now.ToString();
var food = Request.Form["options1"];
var drinks = Request.Form["options2"];
string[] data = new String[4] {linedesc, datemade, food, drinks};
// Go back to Default.html and keep values selected, ie. values don't reset
var blah = "hi";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (IsPost)
{
ViewModels.Default vm = new ViewModels.Default(linedesc, food, drinks, null);
vm.Save();
}
Json.Write(data, Response.Output);
}
Default.html (View)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing Stuff</title>
<script type="text/javascript" src="/Views/JS/jquery-2.0.3.js"></script>
<script type="text/javascript" src="/Views/JS/Default.js"></script>
</head>
<body>
<form id="mainpage" method="post" action="../Controllers/Default">
<div style="clear:both;" id="dbInput">
<label for="linedesc">Description:</label>
<input type="text" id="linedesc" name="linedesc" />
<br /><br />
<input id="fruits" name="options1" value="fruits" type="radio" />
<label for="fruits">Fruits</label>
<input id="candies" name="options1" value="candies" type="radio" />
<label for="candies">Candies</label>
<input id="snacks" name="options1" value="snacks" type="radio" />
<label for="snacks">Snacks</label>
<br /><br />
<label for="options2">Choose beverage:</label>
<select id="options2" name="options2">
<option value="Coca-Cola">Coca-Cola</option>
<option value="Sprite">Sprite</option>
<option value="Root Beer">Root Beer</option>
<option value="Orange Juice">Orange Juice</option>
</select>
<br /><br />
<label for="options3">Sample:</label>
<select id="options3" name="options3">
</select>
<input type="submit" id="submit1" name="submit1" value="Submit" />
</div>
</form>
</body>
</html>
Default.js (JavaScript)
$(document).ready(function () {
//$('#submitText').click(function () {
// $('#txtHolder').html('<span>I am like code behind in txtHolder.</span>');
// $('#results').html('<span>Results are here today.</span>');
//});
$('#mainpage').submit(function (e) {
e.preventDefault();
alert("hi");
var formData = $(this).serialize();
var frm = $(e.target);
$.ajax({
url: "../../Controllers/Default.cshtml",
data: formData,
type: "POST",
dataType: "json",
success: function (response) {
alert(response);
var options3 = $("#options3");
options3.empty();
for (var i = 0; i < response.length; i++)
{
options3.append(
$("<option></option>").text(response[i]).val(response[i])
);
}
// Adds data to dropdown
},
error: function () {
alert("Sorry, there seems to be a problem contacting the server.");
}
});
});
});