2011-11-30 136 views
4

我希望实现的基本功能是在选择下拉列表项目时更新表格的内容。当用户进行新选择并从数据库检索新信息并重新填充表格时,这将会更新。在ASP.Net中渲染局部视图使用Ajax的MVC3 Razor

另外值得一提的是,我想要的.change()的DropDownListFor与给ajaxForm中不包含工作,但在其他地方出现在页面上(当然以另一种形式)

要做到这一点我看着这个问题:Rendering partial view dynamically in ASP.Net MVC3 Razor using Ajax call to Action哪个能够做好我想要做的事情。

到目前为止,我有一个控制器方法,可处理填充定制视图模型的局部视图:

[HttpPost] 
    public ActionResult CompanyBillingBandDetails(int id = 0) 
    { 
     var viewModel = new BillingGroupDetailsViewModel(); 
     var billingGroupBillingBands = 
      _model.GetAllRecordsWhere<BillingGroupBillingBand>(x => x.BillingGroupId == id).ToList(); 

     foreach (var band in billingGroupBillingBands) 
     { 
      viewModel.BillingBands.Add(band.BillingBand); 
     } 

     return PartialView("BillingGroupDetailsPartial", viewModel); 
    } 

视图模型我想填充每个电话:

public class BillingGroupDetailsViewModel 
    { 
     public List<BillingBand> BillingBands { get; set; } 
    } 

强类型模型我使用作为用于局部视图

public class BillingBandsObject 
    { 
     public int BillingBandId { get; set; } 
     public int RangeFrom { get; set; } 
     public int RangeTo { get; set; } 
     public Decimal Charge { get; set; } 
     public int BillingTypeId { get; set; } 
     public bool Delete { get; set; } 
    } 

的杂色一个模型人查看填充和回报:

@model xxx.xxx.DTO.Objects.BillingBandsObject 


<tr> 
    <td> 
     @Html.DisplayTextFor(x => x.RangeFrom) 
    </td> 
</tr> 
<tr> 
    <td> 
     @Html.DisplayTextFor(x => x.RangeTo) 
    </td> 
</tr> 
<tr> 
    <td> 
     @Html.DisplayTextFor(x => x.Charge) 
    </td> 
</tr> 

的页面的这一部分的剃刀代码:

<table> 
     <thead> 
      <tr> 
       <th> 
        Range From 
       </th> 
       <th> 
        Range To 
       </th> 
       <th> 
        Charge 
       </th> 
      </tr> 
     </thead> 
    <tbody> 

    @using (Ajax.BeginForm("CompanyBillingBandDetails", new AjaxOptions() { UpdateTargetId = "details", id = "ajaxform" })) 
    { 
    <div id="details"> 
     @Html.Action("CompanyBillingBandDetails", new { id = 1 }) 
    </div> 
    } 

    </tbody> 
</table> 

,最后我解除几乎直接从Darin的回答功能:

$(function() { 
     $('#billinggrouplist').change(function() { 
      // This event will be triggered when the dropdown list selection changes 

      // We start by fetching the form element. Note that if you have 
      // multiple forms on the page it would be better to provide it 
      // an unique id in the Ajax.BeginForm helper and then use id selector: 
      var form = $('#ajaxform'); 

      // finally we send the AJAX request: 
      $.ajax({ 
       url: form.attr('action'), 
       type: form.attr('method'), 
       data: form.serialize(), 
       success: function (result) { 
        // The AJAX request succeeded and the result variable 
        // will contain the partial HTML returned by the action 
        // we inject it into the div: 
        $('#details').html(result); 
       } 
      }); 
     }); 
    }); 

目前我已经与许多错误作斗争,目前我面临着:

“执行处理程序'System.Web.Mvc.HttpHandlerUtil + ServerExecuteHttpHandlerAsyncWrapper'的子请求时出错。”

但是,我觉得我对整个问题的理解可能缺乏。

任何帮助表示赞赏!

+0

您应该添加围绕相关操作方法的控制器定义。 –

+0

是否存在内部异常? –

+0

@ChrisMarisic我不明白为什么?我正在使用的唯一类变量是“_model”,它是我正在使用的存储库的服务层,并且在那里没有问题。我误解你了吗? – M05Pr1mty

回答

0

此错误表示在呈现子视图时出现异常。可能与你的数据有关,即。 NulLReferenceException

只需附加您的调试器并设置为在抛出异常时中断。

+0

我只能将此问题调试到某个特定点。 Ajax表单中的@ Html.ActionLink通过调用我的DI框架中的GetControllerInstance开始加载,但之后崩溃到我提到的异常。 – M05Pr1mty

+0

@MattTolliday - 只需附加调试器并在动作开始时设置一个断点。 –