TempData Message in different tab

前提是你 提交于 2019-12-11 23:53:50

问题


i want in my page after an action like RegisterUser give a message to client for result.so i use TempData(becase i use RedirectToAction method i cant use viewbag).my problem is that if user open another tab in same time message will show in another tab(any page it can be).how can i solve that??

  @using (@Html.BeginForm("RegisterUser", "UserManagement", FormMethod.Post))
{
    @Html.AntiForgeryToken()
   @Html.Partial("_RegisterPagesMessage")
    <table class="Registertbl">
        <tr>
            <td>نام*</td>
            <td> @Html.TextBoxFor(m => m.FName, new { maxlength = 20})<br />
            </td>               
            <td>سمت*</td>
            <td>@Html.TextBoxFor(m => m.Post, new { maxlength = 200})</td>                
        </tr>
    </table>
    <br />
        <input type="submit" value="Insert" class="insertBtn" />
        @Html.ActionLink("back", "ViewUserList", "UserManagement")
}

//_RegisterPagesMessage

@if (TempData["MessageResult"] == null)
{
    <div id="ErrorContent" class="msg-Red" style="display: none;"></div>  <br />                  
}
else
{
    <div id="ErrorContent"  class="@TempData["cssClass"]"  >
            @Html.Label(TempData["MessageResult"] as string)
    </div>  <br />   
}

//Controller

 [HttpGet]
    public ActionResult RegisterUser()
    {
        return View(new User());
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult RegisterUser(Common.UsersManagement.Entities.User model)
    {
        SetUserManagement();
        var Result = userManagement.RegisterUser(model);
SetMessage(Result.Mode.ToString());
        if (Result.Mode == Common.Extensions.ActionResultMode.Successfully)
        {
                return RedirectToAction("RegisterUser");
        }
         // if not Successfull
        return View(model);
    }    
  protected void SetMessage(string Mode)
        {
            var messageResult = XmlReader.FindMessagekey(Mode);
            TempData["MessageResult"] = messageResult.MessageContent;
            TempData["cssClass"] = messageResult.cssClass;
        }

回答1:


Easy solution. In your RegisterUser controller method check for a value in TempData and transfer it to ViewData, then have the View check the ViewData, which only survives for that one view.

[HttpGet]
public ActionResult RegisterUser()
{
    if( TempData.ContainsKey( "MessageResult" )
    {
        ViewData["MessageResult"] = TempData["MessageResult"];
        ViewData["cssClass"] = messageResult.cssClass;
    }

    return View(new User());
}

Now in the view use ViewData instead of TempData.



来源:https://stackoverflow.com/questions/21980420/tempdata-message-in-different-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!