2011-03-22 55 views

回答

1

你可以使用一個HTML <form>。例如:

<% using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "myForm" })) { %> 
    <input type="text" name="foo" /> 
    <input type="text" name="bar" /> 
    <input type="text" name="baz" /> 
    <input type="submit" value="go go" /> 
<% } %> 

然後AJAXify這種形式:

$(function() { 
    $('#myForm').submit(function() { 
     $.ajax({ 
      url: this.action, 
      type: this.method, 
      data: $(this).serialize(), 
      success: function(result) { 
       alert(result.message); 
      } 
     }); 
     return false; 
    }); 
}); 

,然後有一個控制器動作,將處理提交:如果你想使用普通鏈路

[HttpPost] 
public ActionResult Index(string foo, string bar, string baz) 
{ 
    // TODO: process something ... 
    return Json(new { message = "Thanks for submitting" }); 
} 

而且而不是<form>與提交按鈕不要忘記審查your previous question

0

其實,這是非常基本的HTML表單類的東西:

在HTML:

<form action="/Route/To/Your/Controller/Action" method="post"> 
    <input type="text" name="yourKeyName" /> 
</form> 

通過jQuery:

$.ajax({ 
    type: 'POST', 
    url: '/Route/To/Your/Controller/Action', 
    data: 
    { 
     yourKeyName: $('#yourInputElement').val() 
    }, 
    success: function(data) 
    { 
     // Do Stuff on success 
    } 
});