2012-11-09 47 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