admin管理员组文章数量:1406328
I'm using jQuery to post back to my controller, but i'm wondering how you pass values as parameters in the ActionResult. For example:
I have a jQuery post:
$.post("Home\PostExample")
but i would like to include a value from a dropdown menu:
@Html.DropDownListFor(m => m.Example, Model.Example, new { @id = "exampleCssId" })
into an Actionresult:
[HttpPost]
public ActionResult PostExample(string myString)
{
//TODO: Write contents of ActionResult
}
Any help would be appreciated.
Thanks.
I'm using jQuery to post back to my controller, but i'm wondering how you pass values as parameters in the ActionResult. For example:
I have a jQuery post:
$.post("Home\PostExample")
but i would like to include a value from a dropdown menu:
@Html.DropDownListFor(m => m.Example, Model.Example, new { @id = "exampleCssId" })
into an Actionresult:
[HttpPost]
public ActionResult PostExample(string myString)
{
//TODO: Write contents of ActionResult
}
Any help would be appreciated.
Thanks.
Share Improve this question edited Jun 2, 2011 at 12:28 abatishchev 100k88 gold badges301 silver badges442 bronze badges asked Jun 2, 2011 at 12:09 BigBadDomBigBadDom 2191 gold badge8 silver badges17 bronze badges3 Answers
Reset to default 4I think this should work:
$.post("Home/PostExample", { myString: $("#exampleCssId").val() } );
Here's an example from something I did recently:
function SaveNewGoal() {
var data = { Name_E: $("#NewGoal #Name_E").val(),
Name_F: $("#NewGoal #Name_F").val(),
Desc_E: $("#NewGoal #Desc_E").val(),
Desc_F: $("#NewGoal #Desc_F").val()
};
$.ajax({
url: '@Url.Action("CreateJson", "Goal")',
data: JSON.stringify(data),
success: SaveNewGoalSuccess,
error: SaveNewGoalError,
cache: false,
type: 'POST',
contentType: 'application/json, charset=utf-8',
dataType: 'json'
});
}
function SaveNewGoalSuccess(data, textStatus, jqXHR) {
$("#NewGoalContainer").hide();
// reload the goal list
ReloadGoals();
}
function SaveNewGoalError(jqXHR, textStatus, errorThrown) {
$("#NewGoalResult").text("Error: " + jqXHR.responseText);
}
Adding to grega's answer, you can also make use of callback function if you want to return some data from action method and display it to user.
$.post("Home/PostExample", { myString: $("#exampleCssId").val() }, function(result){
alert(result);
});
本文标签: cUsing jQuery to post back to a controllerStack Overflow
版权声明:本文标题:c# - Using jQuery to post back to a controller - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745004533a2637179.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论