2010-04-07 76 views
0

我有以下网址:传递URL参数和表单数据一起

http://localhost:49970/Messages/Index/9999

而在视图“索引”我有一个表格,我发布表单数据的动作指数(除芯与[HttpPost ])使用jQuery,像这样:

检视:

<script type="text/javascript"> 
    function getMessages() { 
     var URL = "Index"; 
     $.post(
      URL, 
      $("form").serialize(), 
      function(data) { 
       $('#tabela').html(data); 
      } 
     ); 
    } 
</script> 

<% using (Html.BeginForm()) {%> 

     <%=Html.TextArea("Message") %> 

     <input type="button" id="submit" value="Send" onclick="javascript:getMessages();" /> 

    <% } %> 

控制器:

[HttpPost] 
public ActionResult Index(FormCollection collection) 
{ 
    //do something... 
    return PartialView("SomePartialView", someModel); 
} 

我的问题:如何在操作索引中获取参数“9999”和窗体FormCollection?

PS:对不起我的英语:)

回答

0

您需要在routes集合中声明id。然后,在行动中加上一个可怕的名字。

public ActionResult Index(int id, string otherPostData...) 
{ 
    //do something... 
    return PartialView("SomePartialView", someModel); 
} 

看看这里的MVC的URL路由如何工作的细节:

http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

+0

感谢您的快速答复门迪:) 其实,我的参数是一个GUID(像 “08254a2a-7089-46e6-B03D-406a72a34148”)。我尝试像你说要声明的方法: 公众的ActionResult指数(GUID ID,字符串消息?) {// id为null //消息是确定的(有我输入值) 回报PartialView( “SomePartialView”,someModel); } 如果我更改可空的Guid?只有Guid,当我按下按钮什么都没有发生...... – Fabio 2010-04-07 02:59:10

0

一个简单的方法是将Id添加到您的表单值。

这应该做的伎俩:

<% using (Html.BeginForm()) {%> 

    <%=Html.Hidden("Id") %> 

    <%=Html.TextArea("Message") %> 

    <input type="button" id="submit" value="Send" onclick="javascript:getMessages();" /> 

<% } %> 

如果不工作,你需要的Id抛入ViewData控制器。

另一种方式来做到这一点(这可能是清洁剂)是从表单的action属性得到URL jQuery的职位,。当你在它的时候,我也会让js不显眼。

HTHS,
查尔斯