开发者

How to Display a Status Message on the Next Page after a Redirect with .net Framework?

I am adding a form to my .net web forms application and I want to redirect the user to another page but display a status message after the redirect such as 'Your articles has been submitted successfully'.

Any good ways of doing this?

I was thinking of doing it with sessions and a user control but wanted to see if there is an easier way.

Thought about code like this:

User control codebehind:

    public String SessionName { get; set; }
    public String Message
    {
        get
        {
            if (Session[SessionName] == null)
                return String.Empty;

            return Session[SessionName].ToString();
        }

    }

    protected void Page_Unload(object sender, EventArgs e)
    {
        Session[SessionName] = null;
    }

开发者_开发问答User control markup:

<% if (!String.IsNullOrEmpty(Message))
   {%>
        <div>
            <%= Message %>
        </div>
<%} %>


No, saving it in session and then reading on another page is the way to go.


What if your redirection included a query-string parameter that the destination page recognized and selected the right message? That avoids session altogether.


Landed on this post when trying to figure out how to pass the default StatusMessage field you get in net6 web apps (Razor Pages in this case).

I got it working with session as suggested above. However, I realised that in my case it was utterly unnecessary: there is a [TempData] attribute you can stick on the StatusMessage property of one page that will store it in memory (I'm guessing) and delete it after you read it.

Therefore, all you need to do is have the field defined in both the caller page and the receiving page and just set it on the caller page - the receiving page will find the value you set on the caller when you try to read it on the html side. Next time you try to read it it will have been deleted (so you don't keep showing the same message over and over).

Example.

Caller page post call:

public class Create : PageModel
{
    private readonly IMyService _service;

    public Create(IMyService service)
    {
        _service = service;
    }

    [TempData] public string StatusMessage { get; set; } = string.Empty;

    public async Task<IActionResult> OnPostAsync()
    {
        var model = new SomeModel();
        try
        {
            await _service.Create(model);
        }
        catch(Exception e)
        {
            StatusMessage = $"Error: {e.Message}";
            return Page();
        }
        StatusMessage = $"Model created successfully";
        return RedirectToPage("/SomeModels/Index");
    }
}

Receiving page:

public class Index : PageModel
{
    [TempData] public string StatusMessage { get; set; } = string.Empty;

    public readonly List<ExerciseCategory> Models;

    public async Task<IActionResult> OnGet()
    {
        var models = await _service.Get();
        Models.AddRange(models);
        return Page();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜