如何正确地将新的Guid通过View模型传递给EF模型

时间:2019-10-22 16:08:57

标签: c# entity-framework asp.net-core

我正在尝试构建一种方法,该方法可将Guid创建并插入到先前保存的媒体文件的数据库中,并将其插入到项目的wwwroot中。我将实体基本上分为三个部分:数据层的EF模型,业务层的服务模型和Web层的ViewModel。

如我所说,文件已保存在Web层的文件夹中,并且我已经成功创建了一种旨在上载和提取压缩文件夹的方法,并将提取的文件馈送到文件夹中经过清理的文件夹结构中wwwroot文件夹。我尝试过的是构建ViewModel并将它们手动映射到EF模型。 VM拥有3个特别称为EF的模型:MediaModel,FolderUpload和Test。

在上载/提取文件夹的方法中,它立即将新的Guid附加到数据库中的文件夹本身,并列出该文件夹的保存位置。就是那个方法。

在我遇到问题的当前方法中,我尝试对媒体文件执行相同的操作,在其上设置布尔“选择”选项,立即为其附加新的GUID,并保存文件夹/文件数据库的位置。当我在其上放置一个断点时,它得到的是空引用。当然,我将添加示例代码来展示正在发生的事情。

来自控制器:

public IActionResult CreateMediaTest([Bind("LocationPath, Selected, MediaID, TestID, FolderName, DataAdded")]CombinedMediaViewModel fm, string path, string Id)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var getTestItem = _service.GetMediaTestByID(Id);
                    var getMediaItem = _service.GetMediaItem(Id);
                    var getFolderByID = _service.GetFolderByID(Id);
                    var getLocationPath = _service.GetLocationPath(path);

                    MediaModel test = new MediaModel
                    {
                        MediaID = getMediaItem.MediaID,
                        TestID = getTestItem.TestID,
                        Selected = true,
                        LocationPath = getLocationPath.ExtractPath,
                        FolderName = fm.Folder.FolderName,
                        DateAdded = fm.Media.DateAdded
                    };

                    var result = _service.CreateMediaTest(test);

                }
                TempData[StaticString.StatusMessage] = "Your have successfully created a test!";
                return RedirectToAction(nameof(UploadConfirmationPage), new { id = fm.MediaID == Guid.Empty });
            }
            catch (Exception ex)
            {

                TempData[StaticString.StatusMessage] = "Error: " + ex.Message;
                return RedirectToAction(nameof(ManageCurrentMediaPage), new { id = fm.MediaID == Guid.Empty });
            }


        }

来自业务层:

        public MediaModel GetMediaItem(string ID)
        {
            return _repo.GetMediaItem(ID);
        }

        public FolderUpload GetFolderByID(string ID)
        {
            return _repo.GetFolderByID(ID);
        }

        public FolderUpload GetLocationPath(string path)
        {
            return _repo.GetLocationPath(path);
        }

这是ViewModel:

    public class CombinedMediaViewModel
    {
        #region Mapped Entity Items
        public MediaModel Media { get; set; }
        public FolderUpload Folder { get; set; }
        public Test Test { get; set; }
        public IdentityUser User { get; set; }
        #endregion

        #region Mapped Frontend Items
        public string FolderPath { get; set; }
        public string FolderName { get; set; }
        public Guid MediaID { get; set; }
        public string UserID { get; set; }
        public DateTime DateCreated { get; set; }
        public Guid TestID { get; set; }
        public bool Selected { get; set; }
        #endregion
    }

这是cshtml视图:

     <div class="d-flex justify-content-between align-items-center mb-3">
                            @if (Model.URL.ToLower() != "/explorer/")
                            {
                                <h4 class="card-title mb-0">@Model.FolderName</h4>
                                <div class="titles">
                                    @await Html.PartialAsync(PartialView.StatusMessage, TempData[StaticString.StatusMessage])
                                </div>
                            }
                        </div>
                        <div class="card-block">

                            <ul>
                                @*Listing the directories *@
                                @foreach (Crowdsource.DataAccess.Models.FolderUpload dir in Model.FolderList)
                                {
                                    <li>
                                        <input type="hidden" asp-for="@dir.FolderID" />
                                        <input type="hidden" asp-for="@dir.FolderName" />
                                        <input type="hidden" asp-for="@dir.ExtractPath" />
                                        <img src="~/img/folder.jpg" alt="Folder" align="top" style="width: 20px; height: 20px; border: none" />
                                        <a href="@(Model.URL + dir.FolderName)/" title="@dir.FolderName">@dir.FolderName</a>
                                        . . . . . . @dir.DateAdded
                                    </li>
                                }
                                @*Listing the files *@
                                @*@foreach (Crowdsource.DataAccess.Models.MediaModel item in Model.MediaItem)
                                    {
                                        <li>
                                            <input type="hidden" asp-for="@item.Test.TestName" />
                                        </li>
                                *@
                                @foreach (Crowdsource.DataAccess.Models.MediaModel file in Model.MediaList)
                                {

                                    <li>

                                        <input type="hidden" asp-for="@file.MediaID" />
                                        <input type="hidden" asp-for="@file.FileName" />
                                        <input type="hidden" asp-for="@file.DateAdded" />
                                        <input type="hidden" asp-for="@file.Folder.FolderName" />
                                        <input type="hidden" asp-for="@file.Test.AID" />
                                        <input type="checkbox" asp-for="@file.Selected" class="form-check-input" />
                                        <img src="~/img/image.png" alt="Folder" align="top" style="width: 20px; height: 20px; border: none" />
                                        <a href="@(Model.URL + file.FileName)"
                                           title="@file.FileName" target="_blank">
                                            @file.FileName
                                        </a>. . . . . . @file.FileSizeText
                                        . . . . . . @file.DateAdded

                                    </li>

                                }
                            </ul>
                            <div class="card-footer">
                                <button type="submit" class="btn btn-inverse-success">Create Test</button>
                                <button type="submit" class="btn btn-inverse-danger">Delete Folder</button>
                            </div>

                        </div>

0 个答案:

没有答案