如何发布一个ViewModel回不是模型MVC使用淘汰赛(How to post a viewMod

2019-10-17 02:52发布

我有以下代码:

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

有谁知道如何更改代码,方便吗?

Answer 1:

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

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

然后,它为改变那么简单

data: ko.toJSON(viewModel),

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

希望这可以帮助。



文章来源: How to post a viewModel back instead of the model MVC using knockout