ASP.NET MVC将空键值对放入控制器

ASP.NET MVC将空键值对放入控制器,asp.net,Asp.net,我有一个类似于KeyValuePair的模型。谢谢大家的帮助,我找到了一个解决方案 public class ViewModelUpdated { public Book book { set; get; } public List<Author> lstAuthors { set; get; } } 公共类视图模型已更新 { 公共图书{set;get;} 公共列表{set;get;} } 只是不要做字典而已:3你试过调试它吗?您的字典是否包含键值对?字典包含值

我有一个类似于
KeyValuePair的模型。谢谢大家的帮助,我找到了一个解决方案

public class ViewModelUpdated
{
    public Book book { set; get; }

    public List<Author> lstAuthors { set; get; }
}
公共类视图模型已更新
{
公共图书{set;get;}
公共列表{set;get;}
}

只是不要做字典而已:3

你试过调试它吗?您的字典是否包含键值对?字典包含值,但在我按save键后,它变为null,我添加了图片为了正确的OOP,一本书应该有一个作者列表(IEnumerable)。你不应该把书/作者作为一个单独的结构。这是一个简单的例子,我有一个包含idBook和idAuthor表的数据库,因为书可以写很多作者,一个作者可以写很多书
public class HomeController : Controller
{
    static ViewModelAuthorsBooks data = new ViewModelAuthorsBooks();
    static Dictionary<Book, List<Author>> tempDict = new Dictionary<Book, List<Author>>();

    public ActionResult Index()
    {
        data = new ViewModelAuthorsBooks();
        data.dictionary = new Dictionary<Book, List<Author>>();

        List<Author> temp = new List<Author>();

        Book book1 = new Book
        {
            Id = 0,
            Name = "Book1",
            Genre = "Genre1",
            Description = "DescriptionDescription",
            Price = 22.42M
        };

        Author author = new Author
        {
            Id = 0,
            Name = "Name1",
            Surname = "Surname1",
            SecondName = "Secondname1"
        };
        temp.Add(author);

        Author author2 = new Author
        {
            Id = 1,
            Name = "Name2",
            Surname = "Surname2",
            SecondName = "Secondname2"
        };
        temp.Add(author2);

        data.dictionary.Add(book1, temp);

        temp = new List<Author>();

        Book book2 = new Book
        {
            Id = 1,
            Name = "Book2",
            Genre = "Genre2",
            Description = "DescriptionDescription2",
            Price = 44.44M
        };

        Author author3 = new Author
        {
            Id = 2,
            Name = "Name3",
            Surname = "Surname3",
            SecondName = "Secondname3"
        };
        temp.Add(author3);

        data.dictionary.Add(book2, temp);
        tempDict = data.dictionary;
        return View(data.dictionary);
    }

    public ActionResult Edit(int id)
    {
        var model = tempDict.FirstOrDefault(x => x.Key.Id == id);

        return View(model);
    }

    [HttpPost]
    public ActionResult Edit(KeyValuePair<Book, List<Author>> data)
    {

        if (ModelState.IsValid)
        {
            return RedirectToAction("Index");
        }
        else
        {
            return View(data);
        }
    }
}
public class Book
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Description { get; set; }
    public string Genre { get; set; }
}

public class Author
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string SecondName { get; set; }
}

public class ViewModelAuthorsBooks
{
    public Dictionary<Book,List<Author>> dictionary { get; set; }
}
@using KeyValuePairTest.Models

@model  IEnumerable<KeyValuePair<Book,List<Author>>>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<div class="panel panel-default">
    <div class="panel-heading">
        Books List
    </div>
    <div class="panel-body">
        <table class="table table-striped table-condensed table-bordered">
            <tr>
                <th class="text-center">
                    @Html.DisplayNameFor(x => x.Key.Id)
                </th>
                <th class="text-center">
                    @Html.DisplayNameFor(x => x.Key.Name)
                </th>
                <th class="text-center">
                    @Html.DisplayNameFor(x => x.Key.Genre)
                </th>
                <th class="text-right">
                    @Html.DisplayNameFor(x => x.Key.Price)
                </th>
                <th class="text-center">
                    Action
                </th>
            </tr>
            @foreach (var item in Model)
            {
                <tr>
                    <td class="text-right">
                        @item.Key.Id
                    </td>
                    <td>
                        @Html.ActionLink(item.Key.Name, "Edit", new { item.Key.Id })
                    </td>

                    <td>
                        @Html.DisplayFor(modelItem => item.Key.Genre)
                    </td>
                    <td class="text-right">
                        @item.Key.Price.ToString("# USD")
                    </td>
                    <td class="text-center">
                        @using (Html.BeginForm("Delete", "Admin"))
                        {
                            @Html.Hidden("id", item.Key.Id)
                            <input type="submit" class="btn btn-default btn-xs" value="Remove" />
                        }
                    </td>
                </tr>
            }
        </table>
    </div>
    <div class="panel-footer">
        @Html.ActionLink("Add", "Create", null, new { @class = "btn btn-default" })
    </div>
</div>
@using KeyValuePairTest.Models

@model  KeyValuePair<Book, List<Author>>

@{
    ViewBag.Title = "Edit";
}

<div class="panel">

    <div class="panel-heading">

        <h5>Edit Book: @Model.Key.Name</h5>

    </div>
    @using (Html.BeginForm("Edit", "Home", FormMethod.Post, new { app = @Model }))
    {
        <div class="panel-body">

            @Html.HiddenFor(b => b.Key.Id)

            <div class="form-group">

                <label>Name:</label>
                @Html.TextBoxFor(x => x.Key.Name, new { @class = "form-control" })
                <input type="text" name="m" />

                <label>Genre:</label>

                @Html.TextBoxFor(x => x.Key.Genre, new { @class = "form-control" })


                <label>Authors:</label>
                @foreach (var author in Model.Value)
                {
                    <label>Name</label>
                    @Html.TextBoxFor(x => author.Name);
                    <label>Surname</label>
                    @Html.TextBoxFor(x => author.Surname);
                    <label>Second name</label>
                    @Html.TextBoxFor(x => author.SecondName);
                    <p></p>
                }
                <label>Description:</label>
                @Html.TextAreaFor(x => x.Key.Description, new { @class = "form-control", rows = 5 })


                <label>Price:</label>
                @Html.TextBoxFor(x => x.Key.Price, new { @class = "form-control" })
            </div>
        </div>

        <div class="panel-footer">
            <input type="submit" value="Save" class="btn btn-primary" />
            @Html.ActionLink("Cancel", "Index", null, new { @class = "btn btn-default" })
        </div>
    }

</div>
public class ViewModelUpdated
{
    public Book book { set; get; }

    public List<Author> lstAuthors { set; get; }
}