I'm new to ASP.net core and I was trying to write a redirect action using #Html.ActionLink
Here is my code :
#page
#model IndexModel
#{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<h2>Main Navigation</h2>
<ul>
<li>#Html.ActionLink("PRIVACY PAGE", "Privacy", "Privacy")</li>
</ul>
</div>
This action link is supposed to redirect to the PrivacyPage, but instead, the values that I pass are being sent as a parameter to the URL
https://localhost:7020/?action=Privacy&controller=Privacy
The expected result is
https://localhost:7020/Privacy
Not sure what I'm missing. Will this ActionLink work only for the MVC projects?
Thank You !!
If i understand you correctly, I think you put wrong controller name.
#Html.ActionLink("link text", "actionname", "controllername")
Instead of #Html.ActionLink("PRIVACY PAGE", "Privacy", "Privacy") it will be #Html.ActionLink("PRIVACY PAGE", "Privacy", "Home")
You can use asp-page attribute with Razor Pages.
<a asp-page="/Privacy">Privacy</a>
if your Privacy page in another folder like /Pages/Home/Privacy
<a asp-page="/Home/Privacy">Privacy</a>
For more documentation https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-6.0
Related
I am using MVC4, C# and visual studio ultimate 2013 in a project.
I am redirecting a user to an index page after submiting a form. However, this webpage has 2 tabs, and I want to redirect the user to the second tab, instead of the first one.
I have a Controller called Material, with an Index action, which sends the user to the Index View.
public ActionResult Index()
{
return View();
}
This View is made of two partial Views, _Materials and _Packages.
#{
ViewBag.Title = "Index";
}
<div class="tabbable">
<ul class="nav nav-tabs">
<li class="active">Materials</li>
<li>Packages</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="Materials">
#Html.Action("Materials", "Material")
</div>
<div class="tab-pane" id="Packages">
#Html.Action("Packages", "Package")
</div>
</div>
</div>
After performing a serie of actions in another section of the application, I which to redirect the user to the /Material page, to the second tab, but I have no idea how to do it !
Currently I am using this code, which always redirects to the first tab:
return RedirectToAction("Index", "Material");
How can I fix my problem?
You can't use something like RedirectToAction because it does not have any capability of appending a URL fragment. What you can do is use something like Redirect which just takes a URL string, and then use the URL helper to still dynamically generate the action URL for you:
return Redirect(Url.Action("Index", "Material") + "#Package");
In other words, Url.Action will generate the main URL for you and return it as a string. You can then append the fragment to this string before finally redirecting.
You'll want to use the
Redirect method
Redirect("www.sitename.com/material/index#Package");
or
string fragment = "Package";
Redirect("www.sitename.com/material/index#" + fragment);
The #Package is what is called a Fragment Identifier, that will focus your browser on the part of the page with the id of Package.
You can simply write an Ajax call to pass to the controller and in case of success, append #tab to the url parameter. for example use this:
I have a view that is created within a MVC area.
from this view i want to add an Html form that gets to an action on a controller that is not in any Area but just in my main controller folder.
#using (Html.BeginForm("MyAction", "MyController", FormMethod.Get))
{
<input type="submit" value="TEST" />
}
When i execute this code it tries to find my controller in the same area and fails, following html is generated
<form action="/MyApp/en/MyController/MyMethod" method="get">
this actually works on other calls:
http://localhost:18183/MyApp/nl/OtherController/OtherAction
My action on the controller without area can be reached in the browser like this:
http://localhost:18183/MyController/MyMethod
But when i add the form the classic way it still doesn't work:
<form action="/MyController/MyMethod" method="get">
How can i achieve to call this controller?
You need to specify an annonymous object to the routeValues attribute:
#using (Html.BeginForm("MyAction", "MyController", new { area = "" }, FormMethod.Get))
{
}
If you wanted to go to a different area rather than no area, modify the empty quotes to be your new area
Documentation for this overload is available at http://msdn.microsoft.com/en-us/library/dd492933(v=vs.118).aspx
I have a simple MVC application that retrieves DB Server, database, username and password from the user to store in an XML file. I want to add a "Test Connection" button to the screen and have it execute a method on the controller called TestConnection. The problem is the TestConnection method resets all the information on the screen when clicked since the View is being returned with no model. (because a GET operation is occurring). Here is my code:
From the Controller (named FrameworkConfigurationController.cs)
public ActionResult TestConnection()
{
return View("Index");
}
[HttpPost]
public ActionResult TestConnection(FrameworkConfigurationViewModel viewModel)
{
// TODO: Test will occur here
viewModel.DbConnectionMessage = string.IsNullOrEmpty(viewModel.DatabaseName) ? "Connection unsuccessful" : "Connection successful";
return View("Index", viewModel);
}
From my View (FrameworkConfiguration/Index):
#model Framework.ViewModels.FrameworkConfigurationViewModel
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>FrameworkConfigurationViewModel</legend>
<div class="editor-label">
#Html.LabelFor(model => model.ServerName)
</div>
#* Edited for brevity *#
<button type="submit" onclick="location.href='#Url.Action("TestConnection")'">
Test Connection</button>
#Html.ValueFor(model => model.DbConnectionMessage)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to Dashboard", "Index", "Home")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
Admittedly, I am new to MVC programming. I'm coming from a Silverlight/MVVM background so the concept is not foreign to me...the disconnected nature of web programming is. I've followed some tutorials out there, but none of them seem to cover this type of thing - every example is contrived. I know how to do this with webforms and code-behind, but I would like to accomplish this with MVC.
Is there some way to "force" the POST operation instead of GET? I was under the impression that
<button type="submit">
accomplished that. Perhaps the implementation of the onclick I have written isn't correct. I am sure this is HTML 101 stuff, but I can't seem to find a simple answer to this.
Thanks for any help you can offer,
Jason
Why not have the Test Connection button trigger an Ajax action to call the controller, and display the result?
That way you avoid submitting your entire page.
In case you're not familiar with the details, here's a good overview on getting started with Ajax and MVC 4
http://ofps.oreilly.com/titles/9781449320317/ch_AJAX.html
It seems that the ViewBag.Title & #RenderSection(..) executes multiple times but i can't figure why it happens
this is a new project, i have just implemented a single controller with a single view
the Index.cshtml contains
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
and the layout:
Thanks in advance :)
Buried beneath your screenshot I can perceive the root of your problem:
<a href="#Html.Action("Inde........
<img src="~/Content/ima....
</a>
You wanted to use Url.Action and not Html.Action which is something entirely different.
<a href="#Url.Action("Inde........
<img src="~/Content/ima....
</a>
So, in example of DotNetOpenAuth I have form in aspx:
<form action="Authenticate?ReturnUrl=<%=HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]) %>" method="post" id="openid_form" %>
</form>
And what will be analog of it aspx in Razor?
#using (Html.BeginForm(---???---)) {}
--- update---
Thanks for all for suggestions, answer is:
#using (Html.BeginForm("Authenticate", "Account", FormMethod.Post,
new { target = "_top", id = "openid_form" })){}
You don't need to call BeginForm; you can still write <form> tags in Razor.
BeginForm is used when posting to MVC routes.
If that is an MVC action, you can write
#using(Html.BeginForm("Authenticate", new { ReturnUrl = Request.QueryString["ReturnUrl"] }))
<form action="Authenticate?ReturnUrl=#Request.QueryString["ReturnUrl"]" method="post" id="openid_form" %>
</form>
There are multiple ways of doing it, though this is the cleanest way that most closely resembles the sample code you posted.