【问题标题】:Error passing between Model and ViewModel?模型和视图模型之间传递错误?
【发布时间】:2021-05-26 09:45:42
【问题描述】:

在上一个问题“传入的ASP .NET Core Repository Id 文章被改为0?”我成功地做到了,但由于与 GitHub 上的团队发生冲突,我不得不重写我的代码。但是,当我运行它时,我在 Model 和 ViewModel 之间出现了传输错误。这是什么,我该如何解决?我不会更改我的代码之间的任何内容。


控制器

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult EditArticle(Article article)
    {
        if(!ModelState.IsValid)
        {
            return View(article);
        }
        if(!_studentRepository.EditArticle(article))
        {
            throw new ArgumentException("...");
        }
        return RedirectToAction("Index");
    }

查看

    @model Megatron.ViewModels.ArticleFacultyViewModel
@{
    ViewData["Title"] = "Edit Article";
}
<div>
    <form asp-action="EditArticle">
        <partial name="_StatusMessage" model="@ViewData["Message"]" />
        <div>
            @Html.HiddenFor(a => a.Article.Id)
            <div class="form-group row">
                <div class="col-2">
                    <label asp-for="Article.Title" class="col-form-label"></label>
                </div>
                <div class="col-5">
                    <input asp-for="Article.Title" class="form-control" />
                </div>
            </div>
            <div class="form-group row">
                <div class="col-2">
                    <label class="col-form-label">Type of contribution</label>
                </div>
                <div class="col-3">
                    <button hidden class="btn btn-primary" type="button" data-toggle="collapse" data-target="#ImportFileCollapse" aria-expanded="false" aria-controls="ImportFileCollapse">
                    </button>
                    <button hidden class="btn btn-primary" type="button" data-toggle="collapse" data-target="#TextAreaCollapse" aria-expanded="false" aria-controls="TextAreaCollapse">
                    </button>
                    <button id="button-collapse" class="btn btn-primary" type="button" data-toggle="collapse" data-target=".multi-collapse" aria-expanded="false" aria-controls="ImportFileCollapse TextAreaCollapse">
                        Switch to Editor
                    </button>
                </div>
            </div>
            

【问题讨论】:

    标签: asp.net-mvc asp.net-core .net-core asp.net-core-mvc repository


    【解决方案1】:

    你在 post 方法中返回一个Artical 类型的模型给 View,

    if(!ModelState.IsValid)
    {
        return View(article);
    }
    

    虽然视图期望 ArticleFacultyViewModel 类型模型。你可以转换它。

    if(!ModelState.IsValid)
    {
        var articleVM = new ArticleFacultyViewModel
        {
            Article = article,
            Faculties = _facultyRepository.GetFaculties()
        };
        return View(articleVM);
    }
    

    【讨论】:

    • 它是正确的,但它不起作用。 article 不会被编辑
    • 就像我说的,我之前的代码有效。但是当我重写它时出现这样的传输错误
    • article will not be edited 是什么意思?你重写了什么?
    • 它不会编辑数据库中的文章,它会返回带有我所做更改的编辑页面。
    • 这意味着模型没有通过验证,ModelState.IsVaild 是假的,所以它返回到你的编辑视图。那么能否顺利收到文章,或许可以给我们展示一下你的文章模型和视图模型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 2015-12-07
    相关资源
    最近更新 更多