I'm using a Html.BeginForm but I need a hyperlink to trigger a postba开发者_如何学Cck to a ActionResult (similar functionality to a LinkButton). I don't think that I can use an ActionLink because i'm not routing to a view with the same name as the ActionResult (or have I misunderstood :S).
Any help would be appreciated.
Thanks
I think you have two options (though the first isn't as flexible and can get messy)
1) style your submit button like a hyperlink (easy, but you'll probably end up using Html.BeginAjax
or something like that)
2) Style a div, ActionLink, or some other element and serialize the form data on posting back using jQuery
If you can better describe the data being returned, we can better customize the dataType
and success
parameters below.
$(function () {
$('#myButton').click(function (e) {
e.preventDefault();
$.ajax({
url: '/MyController/MySuperAction',
type: 'POST',
data: $('#formId').serialize(),
dataType: 'json',
success: function (xhr_data) {
// in this particular example, you'll
// parse your JSON returned.
}
});
});
});
Edit
So, you're controller could look like
public ActionResult MySuperAction(FormCollection form) {
// I don't recommend using FormCollection
// You should stick to the view model pattern
// process your form
return Json(new { MyValue = "Textbox value" });
}
And you'd need to modify the success
function above to something like
success: function(xhr_data) {
$('#MyTextBoxID').val(xhr_data.MyValue);
}
精彩评论