I am working on a relatively simple web application, and my supervisor recommended that I use a combination of Knockout, MVC, and Bootstrap to accomplish my task. Unfortunately, I got stuck on what is probably a trivial part of the project.
I have seen a lot of Knockout tutorials out there, and have been trying to mimic their simplest one, found here: http://learn.knockoutjs.com/#/?tutorial=intro
Unfortunately, none of my data-binding appears to work. The observables never retain their assigned value.
I also ran across this thread while searching, and tried to mimic it, to no avail. Basically, I would just like to know how to properly implement Knockout data binding in Visual Studio MVC4. Here is my HTML code, a lot of which I ripped off from the thread mentioned above.
@model FluidBedSimulation.Models.BedState
@using Newtonsoft.Json
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@if (false)
{
<script src="../../Scripts/jquery-1.9.1.js" type="text/javascript"></script>
<script src="../../Scripts/knockout-2.2.0.js" type="text/javascript"></script>
}
<script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/knockout-2.2.0.js")" type="text/javascript"></script>
<script type="text/javascript">
this.BedMass = ko.observable(1);
this.BedWaterMass = ko.observable(1);
this.binderFraction = ko.observable(1);
(function () {
var model = '@Html.Raw(Json.Encode(Model))';
var viewModel = ko.mapping.fromJS(model);
ko.applyBindings(viewModel);
});
</script>
@*grab values from the view model directly*@
<p>Bed Weight: <strong data-bind="text: BedMass" id="BedMass"></strong></p>
<p>H2O Bed Weight: <strong data-bind="text: BedWaterMass" id="BedWaterMass"></strong></p>
<p>Fraction of Binder in Spray Solution: <strong data-bind="text: binderFraction" id="binderFraction"></strong></p>
<p>
Enter the Bed Mass:
<input data-bind="value: BedMass" />
@*Html.TextBoxFor(model => model.BedMass, new { data_bind = "value: BedMass" })*@
</p>
<p>
Enter the H2O Mass in the Bed:
@Html.TextBoxFor(model => model.BedWaterMass, new { data_bind = "value: BedWaterMass" })
</p>
<p>
Enter the Fraction of Binder in the Spray Solution:
@Html.TextBoxFor(model => model.binderFraction, new { data_bind = "value: binderFraction" })
</p>
<button data-bind="click: Simulate">Simulate</button>
Here is the simple model I have...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FluidBedSimulation.Models
{
public class BedState
{
public double BedMass { get; set; }
public double BedWaterMass { get; set; }
public int binderFraction { get; set; }
public double EvapRate { get; set; }
public double SprayRate { get; set; }
public double AirTemp { get; set; }
public double AirFlow { get; set; }
}
}
And the simple controller. Nothing fancy.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FluidBedSimulation.Models;
using Newtonsoft.Json;
namespace FluidBedSimulation.Controllers
{
public class SimulationController : Controller
{
//
// GET: /Simulation/
public ActionResult Index()
{
BedState state = new BedState
{
BedMass = 0,
BedWaterMass = 0,
binderFraction = 0,
AirFlow = 0,
AirTemp = 0,
EvapRate = 0,
SprayRate = 0
};
FluidBed bed = new FluidBed
{
FluidBedStates = new List<BedState> { state }
};
return View(state);
}
}
}
If you guys could get me started on this thing I would really appreciate it. Otherwise I might just have to stick to good ol' JQuery and html. Thanks in advance!