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"})
Related
I have 1 form and 2 buttons, one within a form, one out side of a form on a page:
<form method="post">
<button asp-page-handler="AddRecord" id="addRecordBtn" class="btn btn-sm
btn-secondary" type="submit">Add Record</button>
</form>
<button asp-page-handler="EmailData" id="emailData" class="btn btn-sm btn-secondary data-btn">Email Data</button>
I've also created some custom validation. When I click the EmailData button, the custom validation fires and returns null as there's no form data. Both are POST.
How do I prevent this from happening? I can't seem to find an up to date answer for .NET Core.
Thanks
Add onsubmit:
<form method="post" onsubmit="return false">
<button asp-page-handler="AddRecord" id="addRecordBtn" class="btn btn-sm
btn-secondary" type="submit">Add Record</button>
</form>
return false will prevent form action
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 am trying to call an action from the controller using onclick method. For some reason it's not returning action I want, it's always jumping to public ActionResult Index() by default.
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Register" class="btn btn-default" onclick="location.href='#Url.Action("RegisterIndex", "Register")'"/>
</div>
</div>
NOTE
I have created view automatically with a model. It's validating input for me by using already generated javascripts. If I change input tag to button it's not gonna do the required validation.
window.location.href does a GET request, that's why it didn't pass your input values to the server.
When you have <input type="submit"> inside a form, clicking it will submit the form with all data you need. I think this is what you want, but you just want it to submit to another action.
To achieve this, I suggest this solution:
Create a hidden field in the form. Its data will be sent to the server.
In your server, base on that hidden value, you can redirect to the appropriate action
Please feel free to ask me if you find anything unclear :)
The <input type="submit">, when inside a form element, will submit the form when clicked unless you return false or event.preventDefault();
returning false will prevent the default behavior for your submit.
EDIT
window.location.href will cause a GET request so your data will not be posted using this method.
HTML
#using (Html.BeginForm())
{
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" id="btnSubmit" value="Register" class="btn btn-default"/>
</div>
</div>
}
Javascript
<script>
$(document).ready(function () {
$("#btnSubmit").click(function () { window.location.href = '#Url.Action("RegisterIndex", "Register")'; return false; });
});
</script>
I have two submit buttons on my form, one to save, one to resolve:
<input type="submit" name="submitButton" value="Save" class="btn btn-default" />
<input type="submit" name="submitButton" value="Resolve" class="btn btn-default" />
I am dealing with these in the Controller like so:
//POST: Exceptions
[HttpPost]
public ActionResult Edit(string submitButton, PersonViewModel personViewModel)
{
switch (submitButton)
{
case "Save":
return (Save(person));
case "Resolve":
return (Resolve(person));
default:
return (View());
}
}
I know that I can make certain properties required by using data annotations on my view model, however I only want these particular properties to be required when the user attempts to Resolve, Save should have no such restrictions.
What is the best way of achieving this?
update
http://weblogs.asp.net/imranbaloch/overriding-unobtrusive-client-side-validation-settings-in-asp-net-mvc-3
The above blog seems to suggest that you can assign an input you wish to be excluded from validating prior to submit by giving it a class and adding some JS like so:
<input id="btnSubmitSave" type="submit" name="submitButton" value="Save" class="btn btn-default ignore" />
<input id="btnSubmitResolve" type="submit" name="submitButton" value="Resolve" class="btn btn-default" />
$(function () {
var settngs = $.data($('form')[0], 'validator').settings;
settngs.ignore = ".ignore";
});
Unfortunately this is still attempting to validate when I click the Save button.
jQuery Validation plugin: disable validation for specified submit buttons
Sigh, it was as simple as giving my Save button the class of cancel wasted a few hours there.
Try using RequiredIf from:
https://foolproof.codeplex.com/
Exactly what you need
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>