我可以通过查看我的模型的文件来发布操作吗?是不是形式的一部分。我只想将myModel.someValue作为参数传递给nextRelease操作,希望不要将它放在表单中的任何位置。
e.g。
查看:
@model myModel
@using (Html.BeginForm("Search", "News", FormMethod.Get, new { id = "myform" }))
{
<div>myModel.someValue</div> //to show it has this field
<script type="text/javascript">
$('#nextbutton').click(function () {
$('#myform').attr("action", "/@controller.Language/news/nextRelease");
$("#submit").click();
});
</script>
}
答案 0 :(得分:3)
当然,你可以使用AJAX:
@model myModel
<script type="text/javascript">
$(function() {
$('#nextbutton').click(function () {
var url = '@Url.Action("NextRelease", "News")';
var dataToPost = @Html.Raw(Json.Encode(new { someValue = Model.SomeValue }));
$.post(url, dataToPost, function(result) {
alert('data successfully posted to server');
});
return false;
});
});
</script>
<button id="nextbutton">Next button</button>
或者如果您不仅要发布单个属性而是发布整个模型:
var url = '@Url.Action("NextRelease", "News")';
var dataToPost = @Html.Raw(Json.Encode(Model));
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(dataToPost),
success: function(result) {
alert('data successfully posted to server');
}
});