How to pass values from a ascx page to controller
how to pass val开发者_如何学编程ues from ascx page contains [ 3 textboxes] to controller
You could use an HTML <form>
. Example:
<% 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" />
<% } %>
and then AJAXify this form:
$(function() {
$('#myForm').submit(function() {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function(result) {
alert(result.message);
}
});
return false;
});
});
and then have a controller action which will handle the submission:
[HttpPost]
public ActionResult Index(string foo, string bar, string baz)
{
// TODO: process something ...
return Json(new { message = "Thanks for submitting" });
}
Also if you want to use a normal link instead of a <form>
with submit button don't forget to review your previous question.
This is actually pretty basic HTML forms kind of stuff:
In HTML:
<form action="/Route/To/Your/Controller/Action" method="post">
<input type="text" name="yourKeyName" />
</form>
Via JQuery:
$.ajax({
type: 'POST',
url: '/Route/To/Your/Controller/Action',
data:
{
yourKeyName: $('#yourInputElement').val()
},
success: function(data)
{
// Do Stuff on success
}
});
精彩评论