2010-12-21 32 views
0

OK我原来的职位:Url.Action处理空值或使用JavaScript客户方值

Url.Action is throwing error CS1026:) expected

得到了回答。但是,我发现模型中我依赖的ID中的一个对象为null。我无法弄清楚如何重写这个工作。

var url = '<%= Url.Action(
       "List", 
       "PlacementAgreementAgencyPlacementAgreementsPlacement", 
       new { 
        Domain = "Placement", 
        Id = Model.Item.PlacementAgreement.PlacementAgreementId, 
        agencyPlacementAgreementId = Model.Item.AgencyPlacementAgreementId, 
        Page = Model.PageNo 
       }) %>'; 

我需要的是这样的(这是目前说在字符文字的字符太多,我认为可能与单/双引号

var url = '<%= Url.Action(
       "List", 
       "PlacementAgreementAgencyPlacementAgreementsPlacement", 
       new { 
        Domain = "Placement", 
        Id = ' + $("#PlacementAgreementId").val() +', 
        agencyPlacementAgreementId = ViewData.Model.AgencyPlacementAgreement == null ? 0 : ViewData.Model.AgencyPlacementAgreement.AgencyPlacementAgreementId, 
        Page = ViewData.Model.PageNo 
       } 
      ) %>'; 

也试过:

var url = '<%= Url.Action(
       "List", 
       "PlacementAgreementAgencyPlacementAgreementsPlacement", 
        new { 
        Domain = "Placement", 
        Id = %>' + $("#PlacementAgreementId").val() +'<%=,  
        agencyPlacementAgreementId = ViewData.Model.AgencyPlacementAgreement == null ? 0 : ViewData.Model.AgencyPlacementAgreement.AgencyPlacementAgreementId, 
        Page = ViewData.Model.PageNo}) %>'; 

但是,javascript和url.action的混合只是让我感到困惑。我的问题是,我的ViewData类中没有包含id的属性..但是如果我在页面上查看源代码,我c看到它正在存储在客户端。

任何想法?谢谢!

回答

0

最终回到原来的位置,传递看起来像老式查询字符串参数。

var url = '<%= Url.Action("List") %>?page=' + pageNo + 
      '&agencyplacementagreementid=' + $("#AgencyPlacementAgreementId").val(); 

所以不知道我是否是以前完全错误的轨道上,你就应该做这种方式 - 或者,如果有做它像我原来的做法的一种方式。但它现在正在工作! :)

0

仁,

你混合客户端的JavaScript与C#服务器端,而不是好混!

我看你这样做是把一个占位符,然后做一个JavaScript替代,类似的唯一方法:

var url = '<%= Url.Action(
      "List", 
      "PlacementAgreementAgencyPlacementAgreementsPlacement", 
       new { 
       Domain = "Placement", 
       Id = 9999,  
       agencyPlacementAgreementId = ViewData.Model.AgencyPlacementAgreement == null ? 0 : ViewData.Model.AgencyPlacementAgreement.AgencyPlacementAgreementId, 
       Page = ViewData.Model.PageNo}) %>'; 

然后做一个replace in Javascript

var realUrl = url.replace('9999', $("#PlacementAgreementId").val()); 
+0

阿好,谢谢:) - 是的,刚开始让我的头绕着MVC和所有这些使用JQuery/Javascript,所以它不会让我感到惊讶,我没有正确地做到这一点!挑战后端开发者 – Jen 2010-12-22 05:18:24