首页 > 解决方案 > 如何让用户直接从网站创建新类别?

问题描述

我创建了一个项目,您可以在其中上传、下载、删除和预览基于(和显示)类别的文件。如何使用户能够在站点内创建和链接新类别?我对此很陌生,不知道该怎么做。这是我的索引页面:

@model IEnumerable<Proiect2PracticaClona.Models.Files>

<center>
    <h1>Suntem fericiti</h1>
    <h2>Ca ne merge proiectu</h2>
    <hr />
    @if (User.IsInRole("Admin"))

    {
<form methord="post" enctype="multipart/form-data" asp-controller="Home" asp-action="Index">
    <input type="file" name="fifile" />
    <div>Alegeti categoria de skill pentru care doriti sa uploadati</div>
    <div>@Html.RadioButton("category", "incepator", "all")<label>Incepatori</label></div>
    <div>@Html.RadioButton("category", "intermediar", "all")<label>Intermediari</label></div>
    <div>@Html.RadioButton("category", "admin", "all")<label>Admini</label></div>
    <input type="submit" value="Uploadati" />
    <hr />
</form>}
    <div>
        File Name:<input id="search"  onkeyup="search()" placeholder="cauta"/>

    </div>

    <table class="table">
        
        <tr>
            <th>Alegeti Categoria:</th>
            <th class="active">@Html.ActionLink("Incepator", "Index", "Home", new { id = "incepator" })</th>
            <th class="active">@Html.ActionLink("Intermediar", "Index", "Home", new { id = "intermediar" })</th>
            <th class="active">@Html.ActionLink("Admin", "Index", "Home", new { id = "admin" })</th>
            
        </tr>

           
                   
        
        <tr>
            <th>File Name</th>

        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td><a href="/Home/DocumentViewer/@item.info">@item.info</a></td>
                <td><a href="@Url.Action("Download","Home",new { filename=item.info })">Download</a></td>
                @if (User.IsInRole("Admin"))
                {
                    <td><a href="@Url.Action("Delete", "Home", new { id = item.id })">Delete</a></td>
                }
                
                
            </tr>
        }
    </table>
    @section scripts
{
        <script>
            function search() {
                $("tr").each(function (index, value) {
                    if (index > 0 && !$(this).find("td")[0].innerText.toLowerCase().includes($("#search").val().toLowerCase())) {
                        $(this).attr("hidden", true);
                    } else {
                        $(this).removeAttr("hidden");
                    }
                    console.log(value);
                })
            }
        </script>
    }
</center>

控制器动作:

[HttpPost]
public async Task<IActionResult> Index(IFormFile fifile, string category)
{
    Category.category.Add(category);

    var fisave = Path.Combine(_iweb.WebRootPath, "Uploads", fifile.FileName);
    var stream = new FileStream(fisave, FileMode.Create);
    await fifile.CopyToAsync(stream);

    Files fileModel=new Files()
    {
        info = fifile.FileName,
        category = category
    };  

    _fileRepository.AddFile(fileModel);
  
    stream.Close();

    files = _fileRepository.GetFileList(); 

    return RedirectToAction("Index",files);
}

标签: c#asp.net-mvcupload

解决方案


推荐阅读