TempData 개체로 저장 또는 수정 완료 메시지 출력하는 코드 조각입니다.

 

Startup.cs

services.AddDistributedMemoryCache();

services.AddSession(options =>
{
    options.IdleTimeout = TimeSpan.FromSeconds(10);
    options.Cookie.HttpOnly = true;
    options.Cookie.IsEssential = true;
});

 

app.UseAuthorization();

app.UseSession();

app.UseEndpoints(endpoints =>

...

 

_StatusMessage.cshtml

@model string

@if (!String.IsNullOrEmpty(Model))
{
    var statusMessageClass = Model.StartsWith("Error") ? "danger" : "success";
    <div class="alert alert-@statusMessageClass alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        @Model
    </div>
}

 

Controller.cs

TempData["StatusMessage"] = "Updated!!!";

 

View.cshtml

@{
    string statusMessage = "";
    if (TempData.ContainsKey("StatusMessage"))
    {
        statusMessage = TempData["StatusMessage"].ToString();
    }
}
<partial name="_StatusMessage" model="statusMessage" />

 

Comments


Comments are closed