1

I am trying to pass a message to another view (actually same controller) I can do it well but there is a problem for me..

I am new on web and it doesnt seem good ..

problem

and here my c# code

if (createStatus == MembershipCreateStatus.Success)
{        
     string registrationMessage = "You have registered successfully";

     return RedirectToAction("KurumsalGiris", new { message = registrationMessage }); 
}


[AllowAnonymous] //sonradan eklendi
public ActionResult KurumsalGiris(string message="")
{

    if (User.Identity.IsAuthenticated)
        return Content("Zaten giriş yapmışsınız");

    ViewBag.RegistrationMessage = message;

    return View();
}

and here html side

@model E_Cv.Models.LogOnModel

@{
    ViewBag.Title = "Kurumsal Giriş";
}

<h2>Kurumsal Giriş</h2>

<h2>@ViewBag.RegistrationMessage</h2>

<p>
  Please enter your user name and password.
  @Html.ActionLink("Yeni Kurum Kaydı", "KurumsalKayit")
   if you don't have an account.
</p>

so I dont know how to pass value to another view with different way.. I dont want to show this kind of message on address bar and user musnt change it.

Secondly Could I do it with "Get" Method?

4 Answers 4

3

Why don't you just return a different view rather than redirecting? In fact, the code the posted in the first place should be posting to a controller that returns a view for a successful login.

In fact, why are you redirecting to a page that asks the user to login if they've just logged in?

Other possible options include encrypting the string in the URL, or just passing a flag in the URL that the controller translates into the corresponding string.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for answer @Jonothan, yes user registered but I need to confirm it via phone. Because this part of the website will be for corporations. Then they musnt be logged until I confirmed them. This is the reason why I did it. And the message will be longer than that one. Then even enyrpted message will not look like good.. Thank you.
2

What you would do is instead of returning a RedirectToAction you could return the View directly: (second parameter is a model, where you can use the same model class E_Cv.Models.LogOnModel adding a RegistrationMessage property to it)

return View("<name of the view>",
   new E_Cv.Models.LogOnModel {
     RegistrationMessage = "You have registered successfully"
   });

or keep the data in the ViewBag like you have done:

ViewBag.RegistrationMessage = "You have registered successfully";
return View("<name of the view>");

regarding your last question, give the message is showing in your URL, you are using the GET method, if you return a View instead of a redirect, it'll avoid showing anything in the URL

4 Comments

mmmm Yes you are right.. I could use View instead of RedirectAction. I think its better than others? thank you
The first part of Johnathan's answer is the same as this, but he then suggested encrypting the string in the URL and that sort of ruined his answer which is why I am giving this one a +1.
@Ricardo, thank you but ViewBag.RegistrationMessage looks empty.. in another view or action .. ?
sorry it was my bad :) yes it works what you say exactly :) thanx. its the most useful technic as I understand and thank you @Johnathan as well
2

You should use TempData in this scenario

if (createStatus == MembershipCreateStatus.Success)
{        
    TempData["Message"] = "You have registered successfully";

    return RedirectToAction("KurumsalGiris"); 
}

And then in your view

@if (TempData["Message"] != null) 
{
    <h2>@TempData["Message"]</h2>
}

Or if you want to do it in the controller, just keep your view the same as it is at the moment and set ViewBag.RegistrationMessage in the controller

ViewBag.RegistrationMessage = TempData["Message"];

4 Comments

wow thats great.. I thought that TempData is similar like ViewBag or ViewData.. then I didnt try it but it works how I want.. What do you prefer using Session or TempData ? thank you
I'd give you +1 for mentioning something that I didn't know about ASP.NET MVC (first time I hear about the TempData class) but after reading what it does and how it works, I think it's a travesty to use it. I just don't see a place for this kind of thing...
I would definately use TempData over Session. Think of TempData as redirect data.
I understand it, thank you so much.. Its very useful information using tempdata.. thanx
1

If the question is how to pass data between controllers without using the querystring, then one option is the Session object.

if (createStatus == MembershipCreateStatus.Success)
{        
    Session["Message"] = "You have registered successfully";

    return RedirectToAction("KurumsalGiris"); 
}


[AllowAnonymous] //sonradan eklendi
public ActionResult KurumsalGiris(string message="")
{
    if (User.Identity.IsAuthenticated)
        return Content("Zaten giriş yapmışsınız");

    ViewBag.RegistrationMessage = (string) Session["Message"];

    return View();
}

However, I agree with @Jonathan Wood below that this is not necessarily the best approach to the particular problem you are attempting to solve. Still, as a general technique, it is worth knowing about.

1 Comment

I didnt think using session ever .. Interesting.. Thank you let me use it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.