我試圖通過字典來操作方法的Html.Post傳遞一個字典作爲在C#中Http.Post參數
Html.BeginForm("ProcessStage2", "Supplier", new {bookVars = Model.BookVars} , FormMethod.Post, new { name = "frmBook" })
內部我可以通過其它性質(INT,字符串。 ..)的模型,但不是字典。有沒有機會實現這一目標?因爲這本字典有29對,看起來太多了,無法分割並將它們分開。
我試圖通過字典來操作方法的Html.Post傳遞一個字典作爲在C#中Http.Post參數
Html.BeginForm("ProcessStage2", "Supplier", new {bookVars = Model.BookVars} , FormMethod.Post, new { name = "frmBook" })
內部我可以通過其它性質(INT,字符串。 ..)的模型,但不是字典。有沒有機會實現這一目標?因爲這本字典有29對,看起來太多了,無法分割並將它們分開。
不,您不能傳遞對象作爲參數。唯一的方法是在數據庫中存儲對象,併爲該對象傳遞僅POST的id。您應該創建包含該字典的模型:
public int id {get;set;}
public Dictionary<string, int> Dictionary {get;set};
並從服務器端通過id加載它。
您可以使用隱藏字段和編輯器模板。讓我們舉個例子:
型號:
public class MyViewModel
{
public IDictionary<string, string> BookVars { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
// fill the dictionary with some dummy data
BookVars = Enumerable
.Range(1, 5)
.ToDictionary(x => "key" + x, x => "value" + x)
};
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
// the model.BookVars property will be properly bound here
return View(model);
}
}
視圖(~/Views/Home/Index.cshtml
):
@model MyViewModel
@using (Html.BeginForm())
{
@Html.EditorFor(x => x.BookVars)
<button type="submit">OK</button>
}
編輯模板(〜/查看/主頁/ EditorTemplates/KeyValuePair`2.cshtml):
@model KeyValuePair<string, string>
@Html.Hidden("Key", Model.Key)
@Html.Hidden("Value", Model.Value)
注意,它會自動呈現在字典中的每個元素的編輯器模板的名稱:KeyValuePair`2.cshtml
對不起,我不能用那麼的編輯器來正確地格式化這一點,但該文件的名稱應是:KeyValuePair[grave accent]2.cshtml
其中[grave accent]
是grave accent character。
另外不要忘了閱讀關於wire format的收藏和字典,默認的模型聯編程序期望更好地瞭解封面下發生了什麼。
序列化您的字典並將其作爲字符串傳遞(可能是base64編碼)。
你可以使用例如json序列化來做到這一點。我推薦這個,因爲它非常靈活,看起來優雅。
您的模型可以包含任何一組對象。你的情況是:
public class MyViewModel
{
public IDictionary<string, string> BookVars { get; set; }
}
在控制器,你可以存根一些數據,我們需要:
public class HomeController : Controller
{
public ActionResult Index()
{
// mock data
var model = new MyViewModel
{BookVars = new Dictionary<string, string> {{"key1", "value1"}, {"key2", "value2"}}};
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
// the binded model is avaliable
return View(model);
}
}
該視圖包含一個表單,包括AJAX提交自定義腳本。這些值可以以任何方式持續和修改。您可以使用隱藏字段。
@model MyViewModel
<script src="/Scripts/script.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('form').frmBookSubmit();
});
</script>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { name = "frmBook" }))
{
<div class="books">
@foreach (var item in Model.BookVars)
{
<input type="hidden" key="@item.Key" value="@item.Value" />
}
</div>
<input type="submit" />
}
腳本。JS,創建使用數據的JSON序列化的AJAX提交一個簡單的jQuery插件:
(function ($) {
$.fn.frmBookSubmit = function() {
var $this = $(this);
if ($this != null && $this != 'undefined' && $this.length > 0) {
$this.submit(function (e) {
e.preventDefault();
var myviewmodel = new Object();
myviewmodel.BookVars = $this.find(".books").booksCollect();
var data = { model: myviewmodel };
var jsonString = JSON.stringify(data);
$.ajax({
url: $this.attr("action"),
type: 'POST',
dataType: 'json',
data: jsonString,
contentType: 'application/json; charset=utf-8'
});
});
}
};
$.fn.booksCollect = function() {
var books = new Array();
$(this).find("input").each(function() {
var k = $(this).attr('key');
var v = $(this).attr('value');
var item = {
Key: k,
Value: v
};
books.push(item);
});
return books;
};
})(jQuery);
如果你覺得有用,你也可以使用例如Newtonsoft.Json庫編寫自定義的JSON粘合劑。
你完成了。