部分视图与Json在MVC3中

时间:2013-07-17 13:32:29

标签: json asp.net-mvc-3 jquery

我正在使用MVC3 Razor我是新手,我被困在了Point点。

在我的代码中,我通过使用Jquery Json传递选定列表数组来进行Ajax调用

按钮单击控制器侧并在那里执行一些操作并尝试

调用“部分视图”我无法在页面上看到部分视图的内容(未获得渲染)。

任何人都可以帮忙解决这个问题。

Jquery代码:

        $(function () {

        $('#btnAdd').click(function () {
                var selectedList = [];
     $("#ddlSupplementalCalculationList option:selected").each(function (i, selected) {
           var $this = $(this);
           selectedList.push({ Id: $this.val(), Value: $this.text() });
       });

     getCalculationListGrid(selectedList)

       });
     });

          function getCalculationListGrid(selectedList) {
         $.ajax(
          {
         url: "AddSelectedList/SupplementalPricing",
           type: "POST",
           dataType: "json",
            traditional: true,    
           data: {selectedList: JSON.stringify(selectedList)},
           success: function (response) {

            }
          });
         }

控制器代码:

        public ActionResult AddSelectedList(string selectedList)
          {
        var js = new JavaScriptSerializer();
        var deserializedSelectedList = (object[])js.DeserializeObject(selectedList);
        var selectedCalculationList = new List<SelectedCalculationListModel>();

        if (deserializedSelectedList != null)
        {
            foreach (Dictionary<string, object> calcList in deserializedSelectedList)
            {
             selectedCalculationList.Add(new SelectedCalculationListModel(calcList));
            }
        }

        List<AssignCalculationsSourceDataModel> lstAssignCalculationsSourceDataModel =

          new List<AssignCalculationsSourceDataModel>();
        AssignCalculationsSourceDataModel assignCalculationsSourceDataModel = new 

        AssignCalculationsSourceDataModel();

          assignCalculationsSourceDataModel.SelectedCalculationNamesCollection = 

         selectedCalculationList;

        lstAssignCalculationsSourceDataModel.Add(assignCalculationsSourceDataModel);



        return PartialView("PartialAssignCalculationGrid", 
                                      lstAssignCalculationsSourceDataModel);

    }

部分视图:

    @model IList<Bnym.Equinox.Accounting.Web.Portal.Models.Pricing.AssignCalculationsSourceDataModel> 

@if(Model!= null) {

  <div id="dvGrid">
    <table id="grid" style="table-layout: fixed;">

            <tr class="gridHeaders">
                <td colspan="13" align="center">
                    Assigned Calculations
                </td>
            </tr>
            <tr class="gridHeaders">
                <td colspan="4" style="width:212px">
                    Assigned Calculations Usage
                </td>
                <td colspan="9" style="width:190px">
                    Hirarchical Pricing
                </td>
            </tr>

        <tr class="gridHeaders">
            <td class="CheckBox" style="width:20px">
                <input type="checkbox" name="SelectAll" id="SelectAll" 
            onclick="CheckAll(this);"
                    title="Select All" />
            </td>
            <td style="width:220px">
                Calculation Name
            </td>
            <td class ="lblCalculationPurpose">
                Calculation Purpose
            </td>
            <td class ="ddlRoundingRules">
                Pricing Calculation Level
            </td>
            <td  class ="ddlRoundingRules">
                Rounding Rule
            </td>
            <td class ="ddlRoundingRules">
                Rounding Type
            </td>
            <td  class = "ddlRoundingPrecision">
                Precision
            </td>
            <td class = "txtAssignCalculation">
                InvestOne Security Identifier
            </td>
            <td class = "txtAssignCalculation">
                HP Rate Source Current
            </td>
            <td class = "txtAssignCalculation">
                HP Rate Source Forward
            </td>
            <td class = "ddlUserBank">
                UserBank
            </td>
            <td class = "txtAssignCalculation">
                HP Rate Time
            </td>
            <td class = "txtAssignCalculation">
                HP Rate Type
            </td>
        </tr>
         <tbody id="dvBody">
            @for (var items = 0; items < Model.Count; items++)
            {
                for (var item = 0; item < Model 
               [items].SelectedCalculationNamesCollection.Count; item++)
                {
                    for (var index = 0; index < 3; index++)
                    {
                        var CalculationPurpose = string.Empty;

                        <tr>
                        <td style="width: 20px">
                        @Html.CheckBox("check", new { @id = "1", @class = "sel", 
                      @onclick = "uncheckHeaderCB(this);" })
                        </td>
                        <td style="width: 220px">
                        @Model[items].SelectedCalculationNamesCollection[item].Value
                    </td>
                        <td class="lblCalculationPurpose">
                        @if (Model[items].SelectedCalculationPurpose.ToLower().Equals
                      ("hp"))
                        {
                            if (index == 0)
                            {
                                CalculationPurpose = "HP – Trading NAV";
                                @Html.Label(CalculationPurpose) 
                            }
                            else if (index == 1)
                            {
                                CalculationPurpose = "HP - Performance NAV";
                                @Html.Label(CalculationPurpose) 
                            }
                        }
                        else if (Model[items].SelectedCalculationPurpose.ToLower
                   ().Equals("dissemination"))
                        {
                            CalculationPurpose = "Dissemination";
                            @Model[items].SelectedCalculationPurpose
                        }
                        else if (Model[items].SelectedCalculationPurpose.ToLower
                     ().Equals("both"))
                        {
                            if (index == 0)
                            {
                                CalculationPurpose = "HP – Trading NAV";
                                @Html.Label(CalculationPurpose) 
                            }
                            else if (index == 1)
                            {
                                CalculationPurpose = "HP - Performance NAV";
                                @Html.Label(CalculationPurpose) 
                            }
                            else if (index == 2)
                            {
                                CalculationPurpose = "Dissemination";
                                @Html.Label(CalculationPurpose) 
                            }
                        }
                    </td>
                        <td class="ddlRoundingRules">
                        @if ((Model[items].SelectedCalculationPurpose.ToLower().Equals
                             ("both") && CalculationPurpose.ToLower().Equals
                     ("dissemination")) || Model
                 [items].SelectedCalculationPurpose.ToLower().Equals("dissemination"))
                        {
                            if (Model[items].PricingCalculationLevelsCollection != 
                         null)
                            {
                                @Html.DropDownList("ddlCalculationLevel", new 
                  SelectList((System.Collections.IEnumerable)Model
                    [items].PricingCalculationLevelsCollection, "id", "value"), new { 
                       @class 
                      = "ddlRoundingRules" })
                            }
                        }
                        </td>
                    @if ((Model[items].SelectedCalculationPurpose.ToLower().Equals
               ("both") && !CalculationPurpose.ToLower().Equals("dissemination")) || 
               Model[items].SelectedCalculationPurpose.ToLower().Equals("hp"))
                    {
                        <td class="ddlRoundingRules">
                        @if (Model[items].RoundingRulesCollection != null)
                        {
                            @Html.DropDownList("ddlRoundingRule", new SelectList
            ((System.Collections.IEnumerable)Model
             [items].RoundingRulesCollection, "id", "value"), new { @class 
            = "ddlRoundingRules" })
                        }
                       </td>
                    }
                    else
                    {
                        <td class="tdBackground"></td>
                    }

                    @if ((Model[items].SelectedCalculationPurpose.ToLower().Equals
              ("both") && !CalculationPurpose.ToLower().Equals("dissemination")) ||
               Model[items].SelectedCalculationPurpose.ToLower().Equals("hp"))
                    {
                        <td class="ddlRoundingRules">
                        @if (Model[items].RoundingTypesCollection != null)
                        {
                            @Html.DropDownList("ddlRoundingTypes", new SelectList
                ((System.Collections.IEnumerable)Model
         [items].RoundingTypesCollection, "id", "value"), new { @class 
        = "ddlRoundingRules" })
                        }
                        </td>
                    }
                    else
                    {
                        <td class="tdBackground"></td>
                    }

                    @if ((Model[items].SelectedCalculationPurpose.ToLower().Equals
         ("both") && !CalculationPurpose.ToLower().Equals("dissemination")) || Model
        [items].SelectedCalculationPurpose.ToLower().Equals("hp"))
                    {
                        <td class="ddlRoundingPrecision">
                        @if (Model[items].RoundingPrecisionsCollection != null)
                        {

                            @Html.DropDownList("ddlRoundingPrecision", new SelectList
           ((System.Collections.IEnumerable)Model
           [items].RoundingPrecisionsCollection, "id", "value"), new { @class 
           = "ddlRoundingPrecision" })
                        }
                        </td>
                    }
                    else
                    {
                        <td class="tdBackground"></td>
                    }

                    @if ((Model[items].SelectedCalculationPurpose.ToLower().Equals
             ("both") && !CalculationPurpose.ToLower().Equals("dissemination")) || 
             Model[items].SelectedCalculationPurpose.ToLower().Equals("hp"))
                    {
                        <td class="txtAssignCalculation">
                        @Html.TextBox("txtInvestOneSecurityIdentifier", "", new { 
                  @Class = "txtAssignCalculation" })
                        </td>
                    }
                    else
                    {
                        <td class="tdBackground"></td>
                    }

                    @if ((Model[items].SelectedCalculationPurpose.ToLower().Equals
               ("both") && !CalculationPurpose.ToLower().Equals("dissemination")) || 
               Model[items].SelectedCalculationPurpose.ToLower().Equals("hp"))
                    {
                        <td class="txtAssignCalculation">
                        @Html.TextBox("txtHPRateSourceCurrent", "", new { @Class 
                   = "txtAssignCalculation" })
                        </td>
                    }
                    else
                    {
                        <td class="tdBackground"></td>
                    }
                    @if (Model[items].SelectedCalculationPurpose.ToLower().Equals
                   ("hp"))
                    {
                        <td class="txtAssignCalculation">
                        @Html.TextBox("txtHPRateSourceForward", "", new { @Class 
                = "txtAssignCalculation" })
                        </td>
                    }
                    else
                    {
                        <td class="tdBackground"></td>
                    }


                    @if ((Model[items].SelectedCalculationPurpose.ToLower().Equals
                 ("both") && !CalculationPurpose.ToLower().Equals("dissemination")) ||
               Model[items].SelectedCalculationPurpose.ToLower().Equals("hp"))
                    {
                        <td class="ddlUserBank">
                        @if (Model[items].UserBanksCollection != null)
                        {
                            @Html.DropDownList("ddlUserBank", new SelectList
                ((System.Collections.IEnumerable)Model
              [items].UserBanksCollection, "id", "value"), new { 
                  @class = "ddlUserBank" })
                        }
                        </td>
                    }
                    else
                    {
                        <td class="tdBackground"></td>
                    }

                    @if ((Model[items].SelectedCalculationPurpose.ToLower().Equals
                ("both") || Model[items].SelectedCalculationPurpose.ToLower().Equals
                ("hp")) && CalculationPurpose.Equals("HP - Performance NAV"))
                    {
                        <td class="ddlUserBank">
                        @if (Model[items].HpRateTimeCollection != null)
                        {
                            @Html.DropDownList("ddlHPRateTime", new SelectList
                  ((System.Collections.IEnumerable)Model
                 [items].HpRateTimeCollection, "id", "value"), new { @class 
                  = "ddlUserBank" })
                        }
                        </td>
                    }
                    else
                    {
                        <td class="tdBackground"></td>
                    }


                                           }
                }
            }
                </tbody>
             </table>
               </div>

提前感谢您的帮助

1 个答案:

答案 0 :(得分:1)

在您成功回调中,您需要处理响应并将收到的html插入页面。

success: function (response) {
    $("#id_of_element_to_paste_response").html(response)
}

并更改请求的类型。

dataType: "html",

回答评论。 首先,您需要检查从服务器返回的响应。使用Fiddler或Firebug或其他东西。

如果所有工作正常并且部分视图在客户端成功返回,则只需编辑jQuery代码。

 $(function () {

        $('#btnAdd').click(function () {
                var selectedList = [];
     $("#ddlSupplementalCalculationList option:selected").each(function (i, selected) {
           var $this = $(this);
           selectedList.push({ Id: $this.val(), Value: $this.text() });
       });

     getCalculationListGrid(selectedList)

       });
     });

          function getCalculationListGrid(selectedList) {
         $.ajax(
          {
         url: "AddSelectedList/SupplementalPricing",
           type: "POST",
           dataType: "html", //here edited
            traditional: true,    
           data: {selectedList: JSON.stringify(selectedList)},
           success: function (response) {
$("#id_of_element_to_paste_response").html(response);//here edited
            }
          });
         }