ASP.NET MVC Core POST Action返回NULL模型

时间:2016-09-17 04:00:33

标签: asp.net-mvc entity-framework visual-studio-2015

在我的ASP.NET MVC Core应用程序中,以下GET操作方法Test(...)正确返回相应视图Test.cshtml中的所有记录,但是当我修改视图中的任何项目并单击{{1}时,视图始终将null模型返回到相应的POST操作方法Submit。我正在使用Test(...)EF Core。我正在使用Tag Helpers

模型

VS2015

视图模型

public class BloggingContext : DbContext
{
    public BloggingContext(DbContextOptions<BloggingContext> options)
        : base(options)
    { }

    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public int PostYear { get; set; }
    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

控制器

public class BlogsWithRelatedPostsViewModel
{
    public int BlogID { get; set; }
    public int PostID { get; set; }
    public string Url { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public int PostYear { get; set; }
}

Test.chtml查看

[HttpGet]
public async Task<IActionResult> Test(string returnUrl = null)
{
    ViewData["ReturnUrl"] = returnUrl;
    ViewBag.YearsList = Enumerable.Range(1996, 29).Select(g => new SelectListItem { Value = g.ToString(), Text = g.ToString() }).ToList();

    var qrVM = from b in _context.Blogs
                join p in _context.Posts on b.BlogId equals p.BlogId into bp
                from c in bp.DefaultIfEmpty()
                select new BlogsWithRelatedPostsViewModel { BlogID = b.BlogId, PostID = (c == null ? 0 : c.PostId), Url = b.Url, Title = (c == null ? string.Empty : c.Title), Content = (c == null ? string.Empty : c.Content) };

    return View(await qrVM.ToListAsync());
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Test(IEnumerable<BlogsWithRelatedPostsViewModel> model, string GO, int currentlySelectedIndex, string returnUrl = null)
{
    ViewData["ReturnUrl"] = returnUrl;
    ViewBag.YearsList = Enumerable.Range(1996, 29).Select(g => new SelectListItem { Value = g.ToString(), Text = g.ToString() }).ToList();

    if (!string.IsNullOrEmpty(GO))
    {
        var qrVM = from b in _context.Blogs
                    join p in _context.Posts on b.BlogId equals p.BlogId into bp
                    from c in bp.DefaultIfEmpty()
                    where c == null? true : c.PostYear.Equals(currentlySelectedIndex)
                    select new BlogsWithRelatedPostsViewModel { BlogID = b.BlogId, PostID = (c == null ? 0 : c.PostId), Url = b.Url, Title = (c == null ? string.Empty : c.Title), Content = (c == null ? string.Empty : c.Content) };
        return View(await qrVM.ToListAsync());
    }
    else if (ModelState.IsValid)
    {
        foreach (var item in model)
        {
            var oPost = _context.Posts.Where(r => r.PostId.Equals(item.PostID)).FirstOrDefault();
            if (oPost != null)
            {
                oPost.Title = item.Title;
                oPost.Content = item.Content;
                oPost.PostYear = currentlySelectedIndex;
                oPost.BlogId = item.BlogID;
            }
            else
            {
                if (item.PostID == 0)
                {
                    Post oPostNew = new Post { BlogId = item.BlogID, Title = item.Title, Content = item.Content, PostYear = currentlySelectedIndex };
                    _context.Add(oPostNew);
                }

            }
        }
        await _context.SaveChangesAsync();
        return View(model);
    }

    // If we got this far, something failed, redisplay form
    return View();
}

上述视图的HTML源页面快照

以下是一条记录的快照: @model IEnumerable<ASP_Core_Blogs.Models.BlogPostViewModels.BlogsWithRelatedPostsViewModel> @{ ViewData["Title"] = "Index"; } <div class="row"> <div class="col-md-12"> <form asp-controller="Blogs" asp-action="Test" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post"> @{ IEnumerable<SelectListItem> yearsList = (IEnumerable<SelectListItem>)ViewBag.YearsList; var currentlySelectedIndex = 0; // Currently selected index (usually will come from model) } <strong>Select a Post Year</strong> <h6>Choose a year and a URL to begin:</h6> <label>Year:</label><select asp-for="@currentlySelectedIndex" asp-items="yearsList"></select><input type="submit" class="btn btn-default" name="GO" value="GO" /> <table class="table"> <thead> <tr> <th></th> <th></th> <th>Url</th> <th>Title</th> <th>Content</th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td><input type="hidden" asp-for="@item.BlogID" /></td> <td><input type="hidden" asp-for="@item.PostID" /></td> <td> <input type="text" asp-for="@item.Url" style="border:0;" readonly /> </td> <td> <input asp-for="@item.Title" /> </td> <td> <input asp-for="@item.Content" /> </td> </tr> } </tbody> </table> <button type="submit" class="btn btn-default">Save</button> </form> </div> </div>

0 个答案:

没有答案
相关问题