我有以下代码。
索引.cshtml:
@using System.Web.Script.Serialization
@model MvcApplication3.Models.Person
<script src="../../Scripts/knockout-2.1.0.js" type="text/javascript"></script>
<!-- This is a *view* - HTML markup that defines the appearance of your UI -->
<p>First name: <input data-bind="value: firstName" /></p>
@*<p>Last name: <input data-bind="value: lastName" /></p>*@
<script type="text/javascript">
var initialData = @Html.Raw(
new JavaScriptSerializer().Serialize(Model.FirstName));
alert(initialData);
// The *viewmodel* - JavaScript that defines data and behavior of the UI
function AppViewModel() {
this.firstName = ko.observable(initialData);
// this.lastName = ko.observable("Bertington");
}
// Activates knockout.js
ko.applyBindings(new AppViewModel());
</script>
家庭控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var people = new PeopleEntities();
var person = people.People.First();
return View(person);
}
}
基本上,它的作用是从数据库中加载一个人,并使用敲除为名字创建一个可编辑的字段并将其加载FirstName
到其中。
这工作正常,但是,我也想加载LastName
. 我不确定如何执行此操作,因为我只是在序列化名字。我在想我想序列化整个模型,但不确定如何访问每个名称。