2013-04-25 74 views
2

嗨我有一個名爲data的變量返回以下JSON。Json序列化到剃刀視圖模型類的綁定

{"tblMaster":{"intMasterID":14,"intMasterRegionID":1,"intMasterDistrictID":1,"intMasterEduLevelID":2,"txtMasterName":"Kureme","txtMasterPreciseLocation":"bulinga","txtMasterPostalAddress":"bulinga},"txtproperty1":"null"} 

和我有一個生成的使用MasterBo Model類的剃鬚刀頁面。 在這MasteBo類別i寫的以下代碼

public class MasteBo 
{ 
    public tblMaster tblMaster { get; set; } 
    public string tproperty1 { get; set; } 
} 

剃刀頁代碼

<div id="tabs-1"> 
    <table> 

     <tr> 
      <td>Region:</td> 
      <td style="width:255px"> 

       @Html.DropDownListFor(model => model.tblMaster.intMasterRegionID, (IEnumerable<SelectListItem>)ViewBag.intMasterRegionID, "--Select--", new { id = "tblMaster_intMasterRegionID",style="width:255px" }) 
     </td><td> @Html.ValidationMessageFor(model => model.tblMaster.intMasterRegionID)</td> 
     </tr> 
     <tr> 
      <td>District:</td> 
      <td style="width:255px"> 
       @Html.DropDownListFor(model => model.tblMaster.intMasterDistrictID, (IEnumerable<SelectListItem>)ViewBag.intMasterDistrictID, "--Select--")</td> 
      <td>@Html.ValidationMessageFor(model => model.tblMaster.intMasterDistrictID)</td> 
     </tr> 

     <tr> 
      <td>Education Level</td> 
      <td style="width:255px"> 
       @Html.DropDownListFor(model => model.tblMaster.intMasterEduLevelID, (IEnumerable<SelectListItem>)ViewBag.MasterEducationalLevels, "--Select--") </td> 
      <td> @Html.ValidationMessageFor(model => model.tblMaster.intMasterEduLevelID)</td> 
     </tr> 
     <tr> 
      <td>Master Name:</td> 
      <td style="width:255px"> 
        @Html.DropDownListFor(model => model.tblMaster.intMasterID, (IEnumerable<SelectListItem>)ViewBag.Masters, "--Select--")</td> 
      <td> @Html.ValidationMessageFor(model => model.tblMaster.txtMasterName)</td> 
     </tr> 
     <tr> 
      <td>Precise Location:</td> 
      <td> 
       @Html.EditorFor(model => model.tblMaster.txtMasterPreciseLocation)</td> 
      <td>@Html.ValidationMessageFor(model => model.tblMaster.txtMasterPreciseLocation)</td> 
     </tr> 
     <tr> 
      <td>Postal Address:</td> 
      <td> 
       @Html.TextAreaFor(model => model.tblMaster.txtMasterPostalAddress) </td> 
      <td>@Html.ValidationMessageFor(model => model.tblMaster.txtMasterPostalAddress)</td> 

     </tr> 

     <tr> 
    </table> 
</div> 

現在對AJAX請求的compleation如何NEDD結合上述JSON數據到頁。

+0

您的視圖如何看起來像? – 2013-04-25 04:24:07

+0

我在視圖中的剃鬚刀代碼 – user2318139 2013-04-25 05:30:59

回答

0

不是從控制器操作返回JSON,而是返回一個局部視圖。因此,讓我們把你已經在部分所示的內容:

_MyPartial.cshtml

@model MasteBo 
<table> 
    <tr> 
     <td>Region:</td> 
     <td style="width:255px"> 
      @Html.DropDownListFor(model => model.tblMaster.intMasterRegionID, (IEnumerable<SelectListItem>)ViewBag.intMasterRegionID, "--Select--", new { id = "tblMaster_intMasterRegionID",style="width:255px" }) 
     </td> 
     <td>@Html.ValidationMessageFor(model => model.tblMaster.intMasterRegionID)</td> 
    </tr> 
    <tr> 
     <td>District:</td> 
     <td style="width:255px"> 
      @Html.DropDownListFor(model => model.tblMaster.intMasterDistrictID, (IEnumerable<SelectListItem>)ViewBag.intMasterDistrictID, "--Select--")</td> 
     <td>@Html.ValidationMessageFor(model => model.tblMaster.intMasterDistrictID)</td> 
    </tr> 
    <tr> 
     <td>Education Level</td> 
     <td style="width:255px"> 
      @Html.DropDownListFor(model => model.tblMaster.intMasterEduLevelID, (IEnumerable<SelectListItem>)ViewBag.MasterEducationalLevels, "--Select--") </td> 
     <td>@Html.ValidationMessageFor(model => model.tblMaster.intMasterEduLevelID)</td> 
    </tr> 
    <tr> 
     <td>Master Name:</td> 
     <td style="width:255px"> 
      @Html.DropDownListFor(model => model.tblMaster.intMasterID, (IEnumerable<SelectListItem>)ViewBag.Masters, "--Select--")</td> 
     <td>@Html.ValidationMessageFor(model => model.tblMaster.txtMasterName)</td> 
    </tr> 
    <tr> 
     <td>Precise Location:</td> 
     <td> 
      @Html.EditorFor(model => model.tblMaster.txtMasterPreciseLocation)</td> 
     <td>@Html.ValidationMessageFor(model => model.tblMaster.txtMasterPreciseLocation)</td> 
    </tr> 
    <tr> 
     <td>Postal Address:</td> 
     <td> 
      @Html.TextAreaFor(model => model.tblMaster.txtMasterPostalAddress)</td> 
     <td>@Html.ValidationMessageFor(model => model.tblMaster.txtMasterPostalAddress)</td> 
    </tr> 
</table> 

而在你的主視圖,你將包括渲染此部分:

<div id="tabs-1"> 
    @Html.Partial("_MyPartial") 
</div> 

好了,現在你可以有對某些控制器動作的AJAX請求:

$.ajax({ 
    url: '/somecontroller/someaction', 
    type: 'GET', 
    cache: false, 
    success: function(data) { 
     $('#tabs-1').html(data); 
    } 
}); 

and the controlle r操作將返回部分視圖而不是JSON對象:

public ActionResult SomeAction() 
{ 
    MasteBo model = ... fetch your model the same way you were doing in the action that was returning JSON 
    return PartialView("_MyPartial", model); 
} 
相關問題