I want to move to another page from current
My View
<p class="btn btn-primary btn-lg">#Html.ActionLink("More information", "GoToApps") More information! »</p>
However my button have link from method ActionLink and string "More information". How is method without linkText ?
My method in controller:
public ActionResult GoToApps()
{
return RedirectToAction("IndexApps", "Apps");
}
This way is run, but I want better display.
Cheers,
If you need only Link you should use #Url.Action("GoToApps")
<form action="#Html.ActionLink("More information", "GoToApps")">
<button type="button" class="btn btn-primary btn-lg"> More information! »</button>
</form>
Related
I have a contact form that is in C# - Razor and built into the Umbraco Scaffold CMS.
Everything almost works .. except after a successful submission - if I refresh the page the web declares I must resubmit the information - clicking ok then causes the page to attempt a refresh but instead of looking for the page as a page it looks for the action method that was called.
How do I get it to refresh the proper location
<button class="btn btn-primary"
id="btnSubmit"
type="submit"
value="submit"
data-action='CreateMessage'
style="height: 75px;">
<h2>Submit</h2>
</button>
function onSubmit(token) {
document.getElementById("form-outer").submit();
}
EDIT
if (ViewBag.Contact != null)
{
[mxmissile THIS SHOULD BE A REDIRECT? instead of simple content ..]
var viewModel = (MyWeb.ContactModel)ViewBag.Contact;
<div id="form-outer" class="contact-confirmation">
<div>
<h2>
<p>Thank you for your message.</p>
</h2>
<p>We will be in touch soon.</p>
</div>
</div>
}
else
{
Html.RenderPartial("/Views/Partials/ContactView.cshtml");
}
I'm on ASP.NET and I want to navigate to another view when clicking on a button on the HTML page.
<input type="button" value="Back" class="btn btn-default" />
That's my button and I want to make a:
return View();
Your button needs to post back some data?
As the most basic response for your question is :
<input type="button" value="Back" class="btn btn-default" onclick='window.location.href = #Url.Action("action","controller")' />
Which will redirect your page to your action.
I have a HomeController and this controller includes Login Action.
I want to go that Action when I click the button but there is an error, unterminated string constant.
How can I fix it?
<a class="btn btn-lg btn-primary btn-block" onclick="location.href='#Url.Action("Login","Home")'">LOGIN</a>
You do not need to set value to location.href using javascript in this case as this is an anchor tag and as long as you have a valid href property value, it will be redirected to that on clicking on the link (unless you are overriding the click behavior and doing something (preventing the default behavior) using javascript.
You should be doing
<a class="btn btn-lg btn-primary btn-block" href="#Url.Action("Login","Home")">LOGIN</a>
Or using the Html.ActionLink Html helper method to generate the anchor tag
#Html.ActionLink("Login", "Login", "Home", null ,
new {#class="btn btn-lg btn-primary btn-block"})
I have a cancel button in a form:
#using (Html.BeginForm("ConfirmBid","Auction"))
{
some stuff ...
<input type="image" src="../../Content/css/img/btn-submit.png" class="btn-form" />
<input type="image" src="../../Content/css/img/btn-cancel.png" class="btn-form" />
}
The issue is I want this button to go to a particular view when I click on it. How do I do this?
Either you can convert the Cancel button as an anchor tag with #Html.ActionLink helper method and apply a css class which makes the link to looks like a button and then in the controller action for that link, you can return the specific view.
#Html.ActionLink("Cancel","Index","Products",null, new { #class="clsButtonFake"})
or
Use 2 submit buttons in the form. One for real submit and one for the cancel. and in your controller action, check which button called the action method.
You can read more about it here in this answer.
Lot of the answers worked in either of the browsers, chrome or ie but not all.
This worked in all -
<input type="button" value="Cancel" onclick="location.href='#Url.Action("Index","Home")';"/>
This is my button HTML:
<button type="button"
class="btn btn-inverse"
id="cancel"
onclick="window.history.back()">
<i class="icon-remove icon-large"></i>
<br />#Localization.Cancel
</button>
Then to customize the onclick attribute in some views I do this:
<script>
$(document).ready(function ()
{
$("#cancel").
attr("onClick",
"document.location.href='#Html.Raw(Url.Action("Index", "Standard",
new { ManualId = Model.ManualId, ChapterId = Model.ChapterId }))'");
});
</script>
Or a styled submit button:
<input type="submit" value="Save Form" name="Save" class="submit-button btn-form" />
Then Javascript for cancel button:
<input type="button" onclick="document.location.href('Home/Index')" value="Cancel" class="cancel-button btn-form" />
// Note: This avoids any of the validation that may happen in the model that
// normally gets triggered with a submit
So with Shyju's appraoch, you use the built in MVC ActionLink helper. Doing this, you'll need to have any images or icons done through css. However, this is much more cachable, especially if you use base64 strings for your images in css.
I like Adauto's approach because it gives you much more control of the markup. MVC Html Helpers are nice, but they still seem to have their heads in the WebForms mindset of "don't worry about it, we'll take care of it for you".
The one thing I would add is Url.Content.
<img src="#Url.Content("~/Content/css/img/btn-submit.png" class="btn-form" />
It's never really a good idea to make your views have to know the location of content relative to it's location.
<a href="/Auction/[ActionName]">
<input type="image" src="#Url.Content("~/Content/css/img/btn-cancel.png")" class="btn-form" />
</a>
if you want to preserve its look as a button, you could do something like this:
<a href="/Auction/[ActionName]">
<input type="button" value="Cancel">
</a>
where [ActionName] is the name of the action that will return your desired view.
<img src="../../Content/css/img/btn-submit.png" class="btn-form" />
I ended up making a helper so I could reuse the cancel button. I added a js confirm in case people click the cancel button by accident after filling in the form.
#helper FormCancelButton(string cancelUrl)
{
<button type="button" class="btn" onclick="if (confirm('Cancel changes?')) location.href = '#cancelUrl';">Cancel</button>
}
I then call it like so:
#FormCancelButton(Url.Action("Index", "User" ))
If you are really keen you could try and detect the dirty state of the form too and only show the confirm dialog if the form had been changed.
<asp:Button runat="server" class="btn btn-danger"
CausesValidation="false" onclick="Cancel_Click" Text="Cancel"/>
protected void Cancel_Click(object sender, EventArgs e)
{
Response.Redirect("Test.aspx");
}
I have a view with a button. When the user clicks the button I want them redirected to a data entry view. How do I accomplish this? I should mention the views are created, tested, and functioning. I can get to them by typing the url.
I looked for steps explaining how to wire up the onclick event of the button but I'm new to MVC and kinda lost at this point.
Thanks!
It depends on what you mean by button. If it is a link:
<%= Html.ActionLink("some text", "actionName", "controllerName") %>
For posting you could use a form:
<% using(Html.BeginForm("actionName", "controllerName")) { %>
<input type="submit" value="Some text" />
<% } %>
And finally if you have a button:
<input type="button" value="Some text" onclick="window.location.href='<%= Url.Action("actionName", "controllerName") %>';" />
Just as an addition to the other answers, here is the razor engine syntax:
<input type="button" value="Some text" onclick="#("window.location.href='" + #Url.Action("actionName", "controllerName") + "'");" />
or
window.location.href = '#Url.Action("actionName", "controllerName")';
If, like me, you don't like to rely on JavaScript for links on buttons. You can also use a anchor and style it like your buttons using CSS.
Text
if using JQuery, you can do this :
<script type="text/javascript">
$('#buttonid').click(function () {
document.location = '#Url.Action("ActionName","ControllerName")';
});
</script>
It has been my experience that ASP MVC really does not like traditional use of button so much. Instead I use:
<input type="button" class="addYourCSSClassHere" value="WordsOnButton" onclick="window.location= '#Url.Action( "ActionInControllerHere", "ControllerNameHere")'" />
Or, if none of the above works then you can use following approach as it worked for me.
Imagine this is your button
<button class="btn" onclick="NavigateToPdf(${Id});"></button>
I got the value for ${Id} filled using jquery templates. You can use whatever suits your requirement. In the following function, I am setting window.location.href equal to controller name then action name and then finally parameter. I am able to successfully navigate.
function NavigateToPdf(id) {
window.location.href = "Link/Pdf/" + id;
}
I hope it helps.
You can easily wrap your button tag with tag.Using Url.Action() HTML Helper this will get to navigate to one page to another.
<a href='#Url.Action("YourAction", "YourController")'>
<input type='button' value='Dummy Button' />
</a>
If you want to navigate with javascript onclick() function then use
<input type='button' value='Dummy Button' onclick='window.location = "#Url.Action("YourAction", "YourController")";' />
$("#yourbuttonid").click(function(){ document.location = "<%= Url.Action("Youraction") %>";})
RIGHT ANSWER HERE: Answers above are correct (for some of them) but let's make this simple -- with one tag.
I prefer to use input tags, but you can use a button tag
Here's what your solution should look like using HTML:
< input type="button" class="btn btn-info" onclick='window.location.href = "#Url.Action("Index", "ReviewPendingApprovals", routeValues: null)"'/> // can omit or modify routeValues to your liking
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/UsersPage">Users</a>
</li>
Try this