Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字典作为参数在C中的Http.Post中传递#_C#_Asp.net Mvc_Asp.net Mvc 3_Forms_Razor - Fatal编程技术网

C# 将字典作为参数在C中的Http.Post中传递#

C# 将字典作为参数在C中的Http.Post中传递#,c#,asp.net-mvc,asp.net-mvc-3,forms,razor,C#,Asp.net Mvc,Asp.net Mvc 3,Forms,Razor,我正在尝试将字典传递给Html.Post中的操作方法 Html.BeginForm("ProcessStage2", "Supplier", new {bookVars = Model.BookVars} , FormMethod.Post, new { name = "frmBook" }) 我可以传递模型的其他属性(int,string,…),但不能传递字典。有没有机会做到这一点?因为字典有29对,它们似乎太多,无法拆分并分开传递。否,不能将对象作为参数传递。唯一的方法是将对象存储在数据库

我正在尝试将字典传递给Html.Post中的操作方法

Html.BeginForm("ProcessStage2", "Supplier", new {bookVars = Model.BookVars} , FormMethod.Post, new { name = "frmBook" })

我可以传递模型的其他属性(int,string,…),但不能传递字典。有没有机会做到这一点?因为字典有29对,它们似乎太多,无法拆分并分开传递。

否,不能将对象作为参数传递。唯一的方法是将对象存储在数据库中,并传入该对象的POST Only id。您应该创建包含该字典的模型:

public int id {get;set;}
public Dictionary<string, int> Dictionary {get;set};
public int id{get;set;}
公共字典{get;set};

然后从服务器端按id加载它。

您可以使用隐藏字段和编辑器模板。让我们举一个例子:

型号:

public class MyViewModel
{
    public IDictionary<string, string> BookVars { get; set; }
}
视图(
~/Views/Home/Index.cshtml
):

@model MyViewModel
@使用(Html.BeginForm())
{
@EditorFor(x=>x.BookVars)
好啊
}
编辑器模板(~/Views/Home/EditorTemplates/KeyValuePair`2.cshtml):

@model KeyValuePair
@Html.Hidden(“Key”,Model.Key)
@Html.Hidden(“Value”,Model.Value)
请注意编辑器模板的名称,它将自动为字典的每个元素呈现:KeyValuePair`2.cshtml

很抱歉,我无法使用SO的编辑器正确格式化此文件,但文件名应为:
KeyValuePair[grave accent]2.cshtml
其中
[grave accent]


另外,不要忘记阅读默认模型绑定器希望更好地了解封面下发生的事情的集合和词典的相关信息。

序列化词典并将其作为字符串(可能是base64编码的)传递。

您可以使用例如json序列化来完成此操作。我推荐这个,因为它非常灵活,看起来很优雅

您的模型可以包含任何一组对象。在您的情况下,它是:

public class MyViewModel
{
    public IDictionary<string, string> BookVars { get; set; }
}
如果您觉得它有用,还可以使用例如Newtonsoft.json库编写自定义json绑定器

你完了

@model MyViewModel

@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.BookVars)
    <button type="submit">OK</button>
}
@model KeyValuePair<string, string>

@Html.Hidden("Key", Model.Key)
@Html.Hidden("Value", Model.Value)
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);
    }
}
@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" />
}
(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);