0

On HomeController.cs code :

 if (User.Identity.IsAuthenticated)
            {
                var profile = Profile.GetProfile(User.Identity.Name);
                ViewData["Message"] = "Welcome " + profile.FirstName + "!";
            }
            else
            {
                ViewData["Message"] = "Welcome to myblog.";
                ViewData["RegisterLink"] = "please register <%:Html.ActionLink('Register', 'Register', 'Account')%>.";
            }

If user is not logged in it should have to show welcome to myblog and please register (it must be actionlink)

and on index.aspx inside Content code is:

  <%:  ViewData["RegisterLink"] %>

But I'm failed to generate ActionLink please help.

3 Answers 3

4

There are many different ways to tackle what you want, I prefer the strongly typed view approach but you could try the following:

ViewData["RegisterLink"] = "true";

Then inside your View, if RegisterLink is true, show the ActionLink().

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

2 Comments

where I need to put "To know more about Mavericks System register yourself <%:Html.ActionLink('Register', 'Register', 'Account')%>" ?
What is happening here is that you are setting ViewData[] variables inside your ActionResult which happens to be considered as Server-Side Code. Once these variables are set, your View() is then called. Since the ViewData[] variables has been set before the View actually gets displayed, you can access (and verify) the values inside those variables. So…what this means is that you can, test for the following: if(ViewData[“RegisterLink”] == “true”)…show your Html.ActionLink(…). Keep in mind this code is inside your View not in the Controller.
3

This is how to generate anchor from code

TagBuilder linkBuilder = new TagBuilder("a");
linkBuilder.MergeAttribute("href", Url.Action("Register", "Account"));
linkBuilder.SetInnerText("Please Register");
ViewData["RegisterLink"] = linkBuilder.ToString();

Comments

3

I would like to avoid using ViewData and use a strongly typed ViewModel to handle this.

My ViewModel will be like this

public class LoggedInUserViewModel : 
{

  public bool IsValid { set;get;}
  public string DisplayName { set;get;}

}

in the controller, check the user is authenticated, and set the properties of the ViewModel object and pass the ViewModel object to the View

LoggedInUserViewModel objVM=new LoggedInUserViewModel();
if (User.Identity.IsAuthenticated)
{
   objVM.IsValid=true;
   objVm.DisplayName="Name from your user entity"

}
return View(objVm);

and in the View

 @if(Model.IsValid) 
 {
   @Html.ActionLink("Register", "Register", "Users")
 }
 else
 {
  Welcome @Model.DisplayName
   @Html.ActionLink("Profile", "Profile", "Users")
 }

Comments

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.