2015-04-20 98 views
1

我有类型的对象MVC模式窗口数据绑定

public class Customer 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public List<Person> Personel { get; set; } 
    public List<Address> Addresses { get; set; } 
} 

其中

public class Person 
{ 
    public int Id { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

public class Address 
{ 
    public int Id { get; set; } 
    public string ZipCode { get; set; } 
    public string City { get; set; } 
    public string Country { get; set; } 
} 
我的主页上

我列出所有的客户作为总结

@model List<Customer> 
<table> 
    <thead> 
     <tr> 
      <th>Customer No</th> 
      <th>Customer Name</th> 
      <th># of personel</th> 
      <th># of addresses</th> 
     </tr> 
    </thead> 
    <tbody> 
     @if(Model != null && Model.Count > 0) 
     { 
      foreach (Customer c in Model) 
      { 
       <tr> 
        <td>@Html.DisplayFor(x => c.Id)</td> 
        <td>@Html.DisplayFor(x => c.Name)</td> 
        <td>@Html.ActionLink("$ " + c.Personel.Count(), "Summary", "Customer", new { onclick = "ShowPersonelDetails(" + item.Id + ")" })</td> 
        <td>@Html.ActionLink("$ " + c.Addresses.Count(), "Summary", "Customer", new { onclick = "ShowAddressDetails(" + item.Id + ")" })</td> 
       </tr> 
      } 
     } 
    </tbody> 
</table> 

当我点击地址计数或个人计数时我想显示一个弹出窗口列出相应的项目。

要列出企业人事(作为一个例子,我有以下的局部视图)

<div id="personel-details" class="ui-modal-window" title="Personeldetails"> 
    <table class="popup-table"> 
     <thead> 
      <tr> 
       <th>Name</th> 
       <th>Surname</th> 
       <th>Place of birth</th> 
       <th>Date of birth</th> 
      </tr> 
    </thead> 
    </table> 
</div> 

为了让我用下面的脚本我的主页

$(function() { 
    $("#balance-details").dialog({ 
     autoOpen: false, 
     resizable: false, 
     height: 300, 
     width: 600, 
     dialogClass: 'no-close', 
     modal: true, 
     buttons: [ 
      { 
       text: "OK", 
       click: function() { 
        $(this).dialog("close"); 
       } 
      }], 
     show: { 
      effect: "fade", 
      duration: 300 
     }, 
     hide: { 
      effect: "puff", 
      duration: 300 
     } 
    }); 
}); 

在该窗口模式,并调用列表我使用下面的代码:

function ShowCurrentBalanceDetails(__id) { 
    var _id = null; 
    if (__id && $.isNumeric(__id)) { 
     _id = parseInt(__id); 
    } 
    if (_id) { 
     $.ajax({ 
      type: "POST", 
      url: "GetPersonelRecords", 
      dataType: "json", 
      data: { 
       id: _id 
      }, 
      success: function (result) { 
       $("#personel-details").html(result); 
       $("#personel-details").dialog("open"); 
      }, 
      error: function (x, t, m, b) { 
       alert(x.responseText); 
      } 
     }); 
    } 
    else { 
     alert("Error!!!!"); 
    } 
} 

重点是我想显示的人员在模态窗口类客户的财产,

  1. 我应该如何在主页
  2. 我应该如何在模态窗口中设置的数据集的局部视图的数据。

我试图将列表设置为数据到局部视图,但我无法设置项目属性。

我该如何完成我的任务?

+0

你能解释一下你已经取得的成就以及你想达到的目标吗?从我的角度来看,这并不完全清楚。 –

回答

1

How should I set the partial view data in the main page

如果你的行动GetPersonelRecords将返回一个PartialView已经与填充的数据,你只要结果放在模式行动

How should I set the data in the modal window.

因为你填充由GetPersonelRecords返回的PartialView的数据,在响应你会得到HTML和您将在HTML模式$("#personel-details").html(result);


PartialView_PersonelPartial.cshtml)看起来几乎相同,主视图。

@model List<Customer> 
<table class="popup-table"> 
<thead> 
    <tr> 
      <th>Name</th> 
      <th>Surname</th> 
      <th>Place of birth</th> 
      <th>Date of birth</th> 
    </tr> 
</thead> 
<tbody> 
    @if(Model != null && Model.Count > 0) 
    { 
     foreach (Customer c in Model) 
     { 
      <tr> 
       <td>@Html.DisplayFor(x => c.Name)</td> 
       <td>@Html.DisplayFor(x => c.Surname)</td> 
       <td>@Html.DisplayFor(x => c.BirthPlace)</td> 
       <td>@Html.DisplayFor(x => c.Birthday)</td> 
      </tr> 
     } 
    } 
</tbody> 
</table> 

在主视图中添加这个网站,我们会从行动结果append数据

<div id="personel-details" class="ui-modal-window" title="Personeldetails"> 

</div> 

你的行动会返回一个PartialView(用HTML rendereder)

public ActionResult GetPersonelRecords(string id) 
{ 
    var personel = // get personel by personId 
    return PartialView("_PersonelPartial", personel); 
} 

javascript ajax调用仍然是相同的,因为将在此行上使用PartialView填充模式$("#personel-details").html(result);还必须通过ge dataType: "html"to receive html

+0

它似乎工作正常,但我有一个问题。虽然ActionResult返回正确的值,并且代码已经成功调用,并且返回的HTTP状态为200(OK),但Ajax调用却出现了错误:代码块。我的代码有什么问题? – user4591106

+0

@ user4591106我认为你得到的错误是因为'dataType:“json”'它必须是'dataType:“html”',让我更新你的进度。 – adricadar

+0

这是正确的。非常感谢你。你的回答非常正确和有帮助。 – user4591106