English 中文(简体)
绑定模型数据到 Knockout 视图模式吗?
原标题:Binding Model data to Knockout ViewModel?

是否可以将您模型中的数据绑绑到淘汰视图模型中。 例如 :

public ActionResult Edit(int id)
{
    Product product = _db.Products.FirstOrDefault(x=>x.ItemId == id);
    return View(product);
}

然后在《观点》中,我通常会做类似的事情:

@model myApp.Models.Product

@using(Html.BeginForm())
{
    @Html.EditorFor(x=>x.ItemName)
    @Html.ValidationMessageFor(x=>x.ItemName)

    <input type="submit" value="Update" />
}

但是,在击倒之后,我会创建一个 EditProductionViewModel 。 从这里,我会做一些事情,比如:

var EditProductViewModel = {
    ItemName = ko.observable(  )
};

EditProductViewModel.Edit = function() {
    $.ajax({
        url: "Home/Edit",
        data: ko.ToJson(this),
        success: function() {
            // do something
        }
    });  
};

$(function() {
    ko.applyBindings(EditProductViewModel);
});

而不是使用Html帮手,

<form data-bind="submit: Edit">
    <input type="text" data-bind="value: ItemName" />
    <input type="submit" value="Update" />
</form>

那么,我如何用从我的控制器上返回的数据 来填充这个数据呢?

问题回答

我对击踢没有经验 但我觉得你不再想 回到你的控制器的风景了

return JSON(product)

这样您就可以在您的笔记本成功功能上 获得产品中的json元素。 您需要收集json元素 。

$.ajax({
    url: "Home/Edit",
    data: ko.ToJson(this),
    success: function(data) {
        // map to knockout view model
    }
});

然后从这里,你就会叫地图装订。

When using knockout you have two ways to do this. 1. Load your textboxes, etc in one view. Upon loading that view for the first time convert your model to JSON upon in initial load to use by knockout.

经由JSON发往/发往JSON的所有额外电话。

You can use in your View: @Html.Raw(Json.Encode(yourModel))

  1. Load your textboxes in your view (ie they are part of your vieW) Trigger off on document.ready() your ajax calls to get your data from your controller, convert to JSON ie return Json(yourModel, JsonRequestBehavior.AllowGet) and bind those results roughly as you are already doing above

注意 - 此方法的缺点是验证。 如果您拥有所有客户端验证, 那么这可以, 因为数据 - * 的属性将会由 MVC 写入您的文本框等 。 如果您有任何服务器侧侧验证, 则在此无法以击倒方式顺利整合 。

There s a decent article here: http://www.codeproject.com/Articles/305308/MVC-Techniques-with-JQuery-JSON-Knockout-and-Cshar but still lacks on server side validation mention.

您可以将数据序列到您的页面上, 然后以服务器的数值初始化 knockout 查看模型 。

ItemName = ko.observable(serializedModel.ItemName);




相关问题
Using jquery to get a partial view and updating the UI

I need to render a partial view (returned from the controller) to show some customer summary details. This will need to happen when the user clicks on a button. In the the mean time the user can ...

MVC 2 / MVC 3 / MVC 4

MVC 2 我们可以轻松地创造领域。 现在,我的问题涉及nes地区(地区内)。

Asp.Net MVC 2 - Changing the PropertyValueRequired string

Using a resx file in the App_GlobalResources directory, I ve been able to change the default message for the PropertyValueInvalid string of the model validators. But it doesn t work to translate the ...

ASP.NET MVC 3 - What features do you want to see? [closed]

I know a bunch of people that are really enjoying the improvements that ASP.NET MVC 2 made over the first release. I have just started to migrate our MVC 1 project over and so far areas has totally ...

热门标签