我正在尝试为新闻及其类别之间的模型建立关系

时间:2018-09-08 16:46:26

标签: html asp.net-mvc model-view-controller asp.net-core

我希望新闻可以有多个新闻类别。 一对多关系。我正在尝试将类别传递给MVC控制器(创建后操作)。但是 ModelState.IsValid 始终失败。我认为我的后期操作无法以某种方式从视图中获取选定的值。

请帮助我的朋友极客!

我有News.cs:

public class News
{
    public long NewsId { get; set; }
    public ICollection<NewsCategory> NewsCategories { get; set; }
}

我有NewsCategory.cs:

public class NewsCategory
{
    public int NewsCategoryId { get; set; }
    public long NewsId { get; set; }
    public string Title { get; set; }
}

我有NewsController.cs:

// GET: News/Create
    public IActionResult Create()
    {
        List<NewsCategory> newsCategories = new List<NewsCategory>();
        newsCategories = (from s in _context.NewsCategory select s).OrderBy(m => m.Title).ToList();
        newsCategories.Insert(0, new NewsCategory { NewsCategoryId = 0, Title = "Select" });
        ViewBag.NewsCategories = newsCategories;
        return View();
    }

这是“创建帖子”操作:

// POST: News/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("Title,Content,NewsTagName1,NewsTagName2")]News news,  List<IFormFile> files)
    {
        if (ModelState.IsValid)
        {
            _context.Add(news);

            var newsCategories = news.NewsCategories;
            foreach(var item in newsCategories)
            {
                item.NewsId = news.NewsId;
                _context.NewsCategory.Add(item);
            }
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(news);
    }

但是总是在'Model.IsValid'上失败 这是对象News中的快照。 enter image description here

这是错误消息: enter image description here

这是新闻创建视图:

@model Carsubfax.Models.News

@{
    ViewData["Title"] = "Create";
}


        <form asp-action="Create" id="comment-form" name="comment-form" method="post" enctype="multipart/form-data">
            <div class="row">
                <div class="col-md-10 col-md-offset-1">
                    <div class="form-group">
                        <label asp-for="Title" class="control-label"></label>
                        <input asp-for="Title" class="form-control" id="txtTitle" autocomplete="off" required="required" placeholder="Enter your news title." />
                        <span asp-validation-for="Title" class="text-danger"></span>
                    </div>
                </div>
                <div class="col-md-10 col-md-offset-1">
                    <div class="form-group">
                        <label class="control-label">News Cover Image<span style="color:red">*</span></label>
                        <input type="file" name="files" required />
                    </div>
                </div>
                <div class="col-md-10 col-md-offset-1">
                    <div class="form-group">
                        <label class="control-label">News Category</label>
                        <select asp-for="NewsCategories" class="form-control" asp-items="@(new SelectList(ViewBag.NewsCategories, "NewsCategoryId", "Title"))" required="required"></select>
                    </div>
                <div class="col-md-10 col-md-offset-1">
                    <div class="form-group text-center">
                        <button type="submit" class="btn btn-primary" onclick="return btnSubmit_clik();">POST</button>
                    </div>
                </div>
                <div class="col-md-10 col-md-offset-1">
                    <div class="form-group text-center">
                        <a asp-action="Index">Back to List</a>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

0 个答案:

没有答案