简体   繁体   中英

How to redirect to a View of another Controller from one Controller's View in ASP.NET MVC 4 Web Application

I am creating a Login page, where there'll be two buttons. One is submit type 'Sign-in' button and other is a redirecting button 'Sign-up' which gets the URL of a view of another controller. This login page (View) belongs to Login Controller and the redirecting button should access the MemberSignup View of the SignUp Controller.

    <p>
     <input type="button" value="Sign Up" class="btn" onclick= "document.location.href='SignUp/MemberSignUp'";/>
   </p>

while running the above code it is executing with the URL: ~/Login/SignUp/MemberSignUp. Thus giving an error as the Login controller should not be included.

您可以按以下方式用作ActionLink

@Html.ActionLink("Sign Up", "MemberSignUp", "SignUp", null, new { @class="your-class" } })

另一个方法是,如果您不想依靠JavaScript来实现按钮上的链接,则可以使用CSS简单地使用锚点并对其进行样式设置,使其看起来像按钮。

<a href="/Controller/View" class="Button">Your button text</a>

Just add a "/" before your url to indicate that the path is absolute, not relative : document.location.href='/SignUp/MemberSignUp'

Or use the razor method to generate the url (best method) : @Url.Action("MemberSignup", "SignUp");

One method is just to point to the correct URL, example below;

 <p> <a href="/SignUp/MemberSignUp" >Sign Up</a> <p> 

or as mentioned below you can use an ActionResult.

However the issue you're having with your code is that you're missing the leading / from your redirect.

Add the following helper method to your view:

@Html.ActionLink("Your link text here", "MemberSignUp", "SignUp")

Moreover if you want some styling you can type:

@Html.ActionLink("Your link text here", "MemberSignUp", "SignUp", null, new{ @class="your-class" })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM