ASP.NET MVC将空KeyValuePair插入控制器

时间:2018-07-17 12:06:48

标签: asp.net

在将更改保存到控制器后,我得到了一个像KeyValuePair<Book, List<Author>>这样的模型。要进行编辑(int id),请输入Book ID。 为此,需要填充KeyValuePair,但是它为空。请帮助。

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

控制器

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>

Dictionary contains keys and values

In edit we see, that dictionary has values, they shown

Change value of Book name

Null

1 个答案:

答案 0 :(得分:0)

非常感谢您,我找到了解决方法

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

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

只是不要做一本全本的字典:3