如何使用knockout发布viewModel而不是模型MVC

时间:2012-08-09 10:17:07

标签: asp.net-mvc knockout.js

我有以下代码:

Index.cshtml:

@using System.Web.Script.Serialization
@model MvcApplication3.ViewModels.PersonViewModel

<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 -->
<form data-bind="submit: save">

    <p>First name: <input data-bind="value: firstName" /></p>
    <p>Last name: <input data-bind="value: lastName" /></p>

    <table>
        <thead>
            <tr>
                      <th>Name</th>
                  </tr>
        </thead>
        <tbody data-bind="foreach: activities">
            <tr>
                <td><input data-bind="value: Name" /></td>
            </tr>    
        </tbody>
    </table>

    <button type="submit">Submit</button>
</form>

<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
    var viewModel = { 
        firstName : ko.observable(initialData.Person.FirstName),
        lastName : ko.observable(initialData.Person.LastName),
        activities : ko.observableArray(initialData.Person.Activities),

        save: function() {
            $.ajax({
                type: "POST",
                url: "/Home/Index",
                data: ko.toJSON(viewModel),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    $("#resultCount").html(data);
                }
            });
        }
    }

    // Activates knockout.js
    ko.applyBindings(viewModel);

</script>

HomeController中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ahb.Insite.HerdRegistration.WebUI;
using MvcApplication3.Models;
using MvcApplication3.ViewModels;

namespace MvcApplication3.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var person = new Person {FirstName = "John", LastName = "Cool"};

            person.Activities = new List<Activity> { new Activity { Name = "Skiing" } };

            var personViewModel = new PersonViewModel (person);

            return View(personViewModel);
        }

        [HttpPost]
        public ActionResult Index(Person person)
        {
            //Save it

            return View();
        }
    }
}

PersonViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcApplication3.Models;

namespace MvcApplication3.ViewModels
{
    public class PersonViewModel
    {
        public Person Person { get; set; }
    }
}

基本上它的作用是为一个人的名字,姓氏和他们所参与的列表中任何活动的名称提供一个可编辑的模板。

因此,此人通过personViewModel发送。

我想在此处进行的更改是,我希望将此人发回到personViewModel中,而不是回发一个人。

有谁知道如何更改代码以方便此操作?

1 个答案:

答案 0 :(得分:3)

这取决于你的PersonViewModel,但如果是这样的话:

class PersonViewModel
{
    public Person Person { get; set; }
    // other properties
}

然后就像改变

一样简单
data: ko.toJSON(viewModel),

data: ko.toJSON( { Person: viewModel, // Other properties } ),

希望这有帮助。