Obtener valores de TempData a la vista

I want to get value of TempData into View

Normativa

[NonAction]
    public ActionResult RedirectToMessagePage(string title, string message)
    {
        TempData["SuccessModel"] = new Models.SuccessModel { Title = title, Message = message };

        return RedirectToAction("Result", "Common");
    }

Ver

if (TempData["SuccessModel"] != null)
        {

            <div id="validationMessages">
                @{
                    var errors =TempData.Values;
                }
                @if (errors != null && errors.Count() > 0)
                {
                    <div style="position:absolute; background:Black; color:White; top:250px; left:550px;">
                        @foreach (var error in errors)
                        {   
                           @error 
                        }
                    </div>
                }
            </div>
        }

Quiero sacar valor de Mensaje of TempData["SuccessModel"] into view and want to display it.

How can I display it?

preguntado el 28 de agosto de 12 a las 14:08

1 Respuestas

bajo

var errors =TempData.Values;

puedes emitir TempData["SuccessModel"] and get it's message

var message = (TempData["SuccessModel"] as Models.SuccessModel).Message;

y usa esto message variable.

Respondido 28 ago 12, 14:08

Thank you I solve my problem by your suggestion. But one problem is there when I refresh my page this message is displaying. I want to remove this message when I refresh my page. so how can I remove it? - Ajay

@Ajay As it's in TempData, it will display only once, if you don't put anything inside again while refreshing (passing again in through your RedirectToMessagePage Action). More code, or more precision would be needed to answer better... - Rafael Althaus

ok. I have one form which contain textboxs and button. When I click on refresh button the value in that text box doesn't get clear. How can I clear that values. Is there any method for clearing values from textboxes? thank you for reply - Ajay

@Ajay : well, Javascript, or a "Clear" method. If it's not related with the TempData problem, it would be easier to ask a new question with example code. I don't see if Refresh button is a button on your own or the navigator's refresh (F5). If it has to do with TempData, I still don't understand (message is in a TextBox ?) - Rafael Althaus

Ok I will ask this problem in other question Thank you for your help - Ajay

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.