如何在局部视图中获取淘汰数据?

时间:2014-08-23 12:38:19

标签: javascript jquery ajax asp.net-mvc-4 knockout.js

我有一个主视图,当点击编辑按钮波纹管功能时会调用:

function ShowEditRecord(e) {
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
$.ajax(
    {
        url: '/Home/TestEdit/'+dataItem.Id.toString(),
        contentType: 'application/html; charset=utf-8',
        type: 'Get',
        dataType: 'html'
    })
.success(function(result)
{ $('#EditTestSection').html(result); })

现在我的部分视图使用淘汰赛,我的部分观点是:

@model KendoSample.Models.Test

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "frm" }))
{

    <legend>Person</legend>

     <input  type="hidden"  id="Id" name="Id" data-bind='value: Id, uniqueName:true'   class='required' />

    <div class="editor-label">
      <label for="Name"/>
    </div>
    <div class="editor-field">
    <input  id="Name" name="Name"  data-bind='value: Name, uniqueName: true'  type="text" class='required'/>
        @Html.EditorFor(model => model.Name);
    </div>

    <div class="editor-label">
      <label for="Family"/>
    </div>
    <div class="editor-field">
    <input  id="Family" name="Family"  data-bind='value: Family, uniqueName: true'  class='required'/>
    </div>

   }
   <input id="btnSubmits" type="button" value="btnSubmit" />
   <script type="text/javascript" src="~/Scripts/knockout-3.0.0.js"/>
   <script type="text/javascript">
   $('#btnSubmits').on('click',function() {
    alert(viewModel.Name().toString());
    });
    var viewModel={
    Id:ko.observable(@Html.Raw(Model.Id.ToString())),
    Name: ko.observable(@Html.Raw(Model.Name.ToString())),
    Family: ko.observable(@Html.Raw(Model.Family.ToString()))
   }
   ko.applyBindings(viewModel);
  </script>

我的控制器代码是:

 public ActionResult TestEdit(Int64 Id)
    {
        var modelItem=getT().Where(a => a.Id == Id).FirstOrDefault();
        return View (modelItem);
    }

现在,我希望在渲染局部视图时填充挖掘视图模型。

1 个答案:

答案 0 :(得分:0)

我使用绑定模型的波纹管代码来淘汰viewModel:

var viewModel={
    Id:ko.observable('@Html.Raw(Model.Id.ToString())'),
    Name: ko.observable('@Html.Raw(Model.Name.ToString())'),
    Family: ko.observable('@Html.Raw(Model.Family.ToString())')
}
ko.applyBindings(viewModel);