如果你絕對要做一個正常的形式提交(非Ajax)的數據在你的js變量,你可以打造爲某種形式的輸入元素你在js變量中的數據,然後你可以使用javascript來提交這個表單。只要元素的名稱與您的操作方法參數匹配,模型綁定就可以工作。
下面是一個簡單的例子,使用jQuery在按鈕單擊事件上發送字符串列表。
$(function() {
var flags = ["aa", "bb", "cc"];
var targetUrl="@Url.Action("Summary","Home")";
$("#SubmitButton").click(function(e) {
//First build a form element and set the action attribute value
var $f = $("<form></form>").attr("action",targetUrl).attr("method","post");
//Loop throug the string and create an input element for each item
$.each(flags, function(a, b) {
var $el = $("<input type='hidden' name='flagsChecked' />").attr("value",b);
//Add the input element to the form
$f.append($el);
});
//Add the form to the page and submit the form
$f.appendTo("body").submit();
});
});
並且在你的http post action方法中,你可以返回一個視圖。
[HttpPost]
public ActionResult Summary(List<string> flagsChecked)
{
return View("Summary");
}
如果你的代碼是UDPATE一些數據,我強烈建議你遵循P-R-G pattern並返回一個重定向響應。
[HttpPost]
public ActionResult Summary(List<string> flagsChecked)
{
return RedirectToAction("Summary");
}
你可以做一個普通郵寄如果表單包含含有在值輸入數組 –
這是一個想法,但不是有點原始? (只是好奇) –
提交表單的標準方式是原始的?你也可以做一個ajax調用,而是返回一個你不使用的視圖,只是返回一個簡單的'JsonResult',否則表示成功,如果成功,使用'location.href'在'success'中重定向。回調 –