1
我有下面的代码将数据发送回控制器MVC:如何利用基因敲除
Index.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));
// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI
function AppViewModel() {
this.firstName = ko.observable(initialData.FirstName);
this.lastName = ko.observable(initialData.LastName);
}
// Activates knockout.js
ko.applyBindings(new AppViewModel());
</script>
的HomeController:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication3.Models;
namespace MvcApplication3.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var people = new PeopleEntities();
var person = people.People.First();
return View(person);
}
[HttpPost]
public ActionResult Index(Person person)
{
//Save it
return View();
}
}
}
基本上,这样做是负载一个来自数据库的人使用knockout为名字和姓氏编辑字段。它将值加载到字段中
这可以正常工作。
但是我不确定如何将更改发回控制器进行保存。他们将不得不反序列化并放回模型中,然后重新发回。不知道如何做到这一点。
任何帮助?