2012-11-09 48 views
0

的操作方法中缺少URL參數值我想將textarea值和其他一些參數傳遞給action方法。所以我用以下方式使用jquery。ASP.Net MVC

查看: -

@Html.TextArea("aboutme") 
<a id="@Profile.Id" title="@Profile.name" onclick="SubmitProfile(this)" > 
Submit</a> 

jQuery的方法: -

function SubmitReview(e,count) {  
    var Text = "'"+jQuery("#aboutme").val()+"'"; 
    var url = 'http://' + window.location.host + '/Controller/ActionMethod?' + 'id=' + e.id  + '&name=' + e.title + '&aboutme=' + Text; 
    jQuery("#Profile").load(url); 

} 

操作方法: -

public ActionResult ActionMethod(string id,string name,string aboutme) 
     { 
      // 
     } 

上面的代碼工作沒錯,但是當有任何一個參數值包含的空格就在其中。在Jquery方法中,URL看起來很好。但在操作方法中,它將值修剪至第一個空格,其餘參數爲空。

讓我有一些例子

讓說的id = '123',名稱= 'amith町' 解釋,aboutme = '我是軟件ENGG'

網址jQuery的方法

url='http://localhost/Controller/ActionMethod?id=123&name=amith cho ,aboutme=i am software engg' 

但越來越行動方法id=123name=amithaboutme=null

如何解決這個問題?

+0

使用此URL發送數據到行動'的http://本地主機/控制器/ ActionMethod ID = 123&名稱= amith町aboutme =我是軟件engg'是** **壞,壞* * '壞'練習......改爲發佈這些值。 – Yasser

回答

3

如果您將它作爲查詢參數傳遞,您應該對輸入的值進行url編碼。

function SubmitReview(e,count) { 
    var Text = jQuery("#aboutme").val(); // quotes aren't necessary 
    var url = 'http://' + window.location.host + '/Controller/ActionMethod?' 
        + 'id=' + e.id 
        + '&name=' + encodeURIComponent(e.title) 
        + '&aboutme=' + encodeURIComponent(Text); 
    jQuery("#Profile").load(url); 

} 

如果字符串包含不encodeURIComponent方法來處理多餘的字符,你可以試試這個功能,從MDN docs引用。用上面的調用替換上面encodeURIComponent的調用。

function fixedEncodeURIComponent (str) { 
    return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A"); 
} 
+0

謝謝tvanfosson ... –

+0

encodeURIComponent()不允許這些特殊字符〜!*()' 所以它正在打破... –

+0

@User_MVC - 更新與文檔的替代建議,包括更多的轉義字符。 – tvanfosson