1

我有以下代码:

索引.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>

家庭控制器:

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();
        }
    }
}

人物视图模型:

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 中。

有谁知道如何更改代码以促进这一点?

4

1 回答 1

3

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

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

然后就像改变一样简单

data: ko.toJSON(viewModel),

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

希望这可以帮助。

于 2012-08-09T10:32:24.547 回答