在ASP.NET中查看数据时不显示错误消息

bvjxkvbb  于 2023-05-19  发布在  .NET
关注(0)|答案(1)|浏览(261)

这是我的控制器:

public async Task<IActionResult> CreatePortfolioCategory(CreatePortfolioCategoryViewModel createPortfolioCategoryViewModel)
    {

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

        var result = await _siteService.CreatePortfolioCategory(createPortfolioCategoryViewModel);
            switch (result)
            {
                case CreatePortfolioCategoryResult.NotFound:
                    ViewBag.ErrorText = "Error";
                    return View(createPortfolioCategoryViewModel);

                case CreatePortfolioCategoryResult.Created:
                    ViewBag.SuccessText = "Successful Create";
                    break;
        }
        return RedirectToAction("Index");
        }

它在数据库中创建成功并重定向到索引,但在索引上不显示此错误和成功消息
我在view上写了这段代码,但是viewbage上的这条消息没有显示在view上。它只显示“成功”

<div>
@if (!string.IsNullOrEmpty(ViewBag.ErrorText))
{
    <div class="alert alert-danger">
        <p>@ViewBag.ErrorText</p>
    </div>
}

@if (!string.IsNullOrEmpty(ViewBag.SuccessText))
{
    <div class="alert alert-success">
        <p>@ViewBag.SuccessText</p>
    </div>
}
<h2> Successfull </h2>

这是service上的create方法:

public async Task<CreatePortfolioCategoryResult> CreatePortfolioCategory(CreatePortfolioCategoryViewModel createPortfolioCategoryViewModel)
    {
        if (createPortfolioCategoryViewModel.ParentId != null && !await _portfolioRepository.IsExistPortfolioCategory(createPortfolioCategoryViewModel.ParentId.Value))
            return  CreatePortfolioCategoryResult.NotFound;

        PortfolioCategory portfolioCategory = new PortfolioCategory()
        {
            PortfolioTitle = createPortfolioCategoryViewModel.PortfolioTitle,
            NameInUrl = createPortfolioCategoryViewModel.NameInUrl,
            IsDelete = createPortfolioCategoryViewModel.IsDelete,
            IsActive = createPortfolioCategoryViewModel.IsActive,
            Order = createPortfolioCategoryViewModel.Order,
            ParentId = createPortfolioCategoryViewModel.ParentId
        };

        await _portfolioRepository.CreatePortfolioCategory(portfolioCategory);
        await _portfolioRepository.SaveChange();
        return CreatePortfolioCategoryResult.Created;

    }
bis0qfac

bis0qfac1#

您没有将ViewBag.SuccessText的值传递给Index方法,因此在Index的视图中,ViewBag.SuccessText始终是空值。
SiteService返回CreatePortfolioCategoryResult.NotFound时,返回的视图是CreatePortfolioCategory.cshtml,当SiteService返回CreatePortfolioCategoryResult.Created时,返回的视图是Index.cshtml。所以只能在Index.cshtml中判断SuccessText是否为空,然后在CreatePortfolioCategory.cshtml中判断ErrorText是否为空。
你可以参考我下面的测试代码:
控制器:

public IActionResult Index(string? successText)
{ 
    ViewBag.SuccessText = successText;
    return View();
}

[HttpGet]
public IActionResult CreatePortfolioCategory()
{ 
    return View();
}

[HttpPost]
public async Task<IActionResult> CreatePortfolioCategory(CreatePortfolioCategoryViewModel createPortfolioCategoryViewModel)
{
    if (!ModelState.IsValid)
    {
        return View(createPortfolioCategoryViewModel);
    }

    var result = _siteService.CreatePortfolioCategory(createPortfolioCategoryViewModel);
    switch (result)
    {
        case CreatePortfolioCategoryResult.NotFound:
            ViewBag.ErrorText = "Error";
            return View(createPortfolioCategoryViewModel);

        case CreatePortfolioCategoryResult.Created:
            ViewBag.SuccessText = "Successful Create";
            break;
    }
    return RedirectToAction("Index", new { successText = ViewBag.SuccessText });
}

Index.cshtml:

<div>
    @if (!string.IsNullOrEmpty(ViewBag.SuccessText))
    {
        <div class="alert alert-success">
            <p>@ViewBag.SuccessText</p>
        </div>
    }
</div>

CreatePortfolioCategory.cshtml:

@if (!string.IsNullOrEmpty(ViewBag.ErrorText))
{
    <div class="alert alert-danger">
        <p>@ViewBag.ErrorText</p>
    </div>
}
<form method="post" asp-action="CreatePortfolioCategory" >
    <div>ParentId</div>
    <div><input type="number" asp-for="@Model.ParentId" /></div>
    <div>PortfolioTitle</div>
    <div><input type="text" asp-for="@Model.PortfolioTitle" /></div>
    <div>NameInUrl</div>
    <div><input type="text" asp-for="@Model.NameInUrl" /></div>
    <div>IsDelete</div>
    <div><input type="checkbox" asp-for="@Model.IsDelete" /></div>
    <div>IsActive</div>
    <div><input type="checkbox" asp-for="@Model.IsActive" /></div>
    <div>Order</div>
    <div><input type="text" asp-for="@Model.Order" /></div>
    <div><input type="submit" value="Submit" /></div>
</form>

测试结果:
返回CreatePortfolioCategoryResult.NotFound时:

返回CreatePortfolioCategoryResult.Created时:

相关问题