Inject model field in onclick event of input in Razor view - razor

My client side scripting is rough and I just started with Razor. I haven't been able to find a way to do the following, where I want to inject a model field value within the onclick confirmation message:
#using (Html.BeginForm("Delete", "ManageLocations", new { id = #Model.Location.Id }))
{ <input type="submit" onclick="return confirm('Are you sure you wish to delete #Model.Location.DisplayLocation ?');" value="Delete">}
What am I missing?

Ooops, stupid mistake...I was embedding this BeginForm within another....The reason this does not work is you cannot have one form nested within another.

Related

Is there a way in HTML (or ColdFusion) to have a radio button trigger a hyperlink without having to submit a form?

I want to have a pair of radio buttons sitting over an HTML table so that whenever the rb is clicked it will cause the table to rebuild (rebuild the page) by submitting a different URL variable. Is there a way to do this without having to build a form and click a submit button? I'm pretty new to this stuff so please keep any answers basic and/or show samples of code. Thanks!
Html is a Markup Language it will not do the logic for you. that way javascript is there .
<input id="gotogoogle" type="radio" name="name" value="google" checked>
<script>
var radiob = document.getElementById("gotogoogle");
radiob.addEventListener("change", function() {
if(radiob.value == "google")
{
document.location = "http:\\www.gooogle.com"
}
});
</script>
Yes there is, you can use Javascript event binding to help you achieve this. This will get you started. This adds an event binding so when you "change" or click the radio buttons, it can fire an event off.
From here you'd need to research how to rebuild the table data in JS if you don't already know.
$("input[#name='nameofinput']").change(function(){
// Do something interesting here
});

MVC HTML posting form data

Apologies in advance if this is a dumb question but I'm struggling to get the look and feel I want whilst posting the data I need to the server: s
I've built a simple file upload page using Dropzone.js and am able to upload files to the server. I also want to pass a value from a drop down to the same action method but can only achieve this if my drop down is contained within my dropzone (which I don't want!)
My Page and HTML look like this:
The class="dropzone" defines where the dropzone is and seems to be have to be attached to the action="~/Media/SaveUploadedFile" or the fallback class gets used instead and screws up the layout. So this code gives me the layout I want with the dropdown outside of the dropzone but does not pass the drop down value to my Action Method: (
What am I doing wrong?
Controller method:
public ActionResult SaveUploadedFile(string ContentDirectory)
Try using Html.BeginForm. Place your dropdown inside of the #using tag.
#using (Html.BeginForm("SaveUploadedFile", "Media", FormMethod.Post, new { #class = "dropzone", enctype = "multipart/form-data" }))
{
//Dropdown here
<div class="fallback">
<input type="file" name="file" />
<input type="submit" value="Upload" />
</div>
}
Here's a similar existing post for uploading files in MVC

rails parameterize form data

I have a rails application that allows searching of posts when a get request in the form of /posts/search/:searchstring is recieved. I have no problems with the functionality when I put the search string in using my browser's url bar, but I am stuck trying to create a search form.
How can I make an html form in rails that allows me to send a get request using the above syntax? Am I going to be stuck using a controller method to redirect? I really want this functionality because I am a firm believer that the URL is a part of the UX and I hate ugly URLs like this:
http://example.com/search?utf8=%E2%9C%93&q=searchstring&commit=Search
A form's gonna do what a form's gonna do, and if you have a form using GET, it's gonna serialize those parameters into the query string.
But since you are using get, you also don't truly need to submit the form. Rather you can just send the browser to the URL.
So for example, somewhere in your JS:
function doSearch(form) {
// This assumes that the first input of your form is the search
// box. YMMV.
var query = encodeURIComponent(form.elements[0].value);
// just send the browser to the constructed URL
window.location = form.action + "/" + query;
// and return false to prevent the actual submit
return false;
}
Then in your html:
<form action = "/posts/search" onsubmit="return doSearch(this)">
<input type="text" name="searchstring" />
<input type="submit" />
</form>
How about adding the form this way:
<form method="get" action="<%= search_posts_url_path %>">#text input here with name "searchstring"</form>

Using MVC, How can I pass an anchor tag on a form submit?

In short, I'm trying to hit an anchor on the page I am submitting to.
So my code looks like:
#using (Html.BeginForm("Times#" + Model.SelectedTimeSort, "Patron", FormMethod.Post, new { id = "frmBuildings" }))
but on the form it comes out as:
<form action="/Times%23Evening" id="frmBuildings" method="post">
How do I set this up to submit and pass an anchor tag?
Assuming you are posting to another MVC method on your application:
You would need to add some JavaScript to the page, then in your ViewModel add an Anchor string property which is set in the MVC controller postback code, when the view loads the javascript (onLoad) can detect the parameter and scroll the page to the anchor:
location.hash = "#" + "#(Model.MyAnchorName)";
Just get rid of the Html.BeginForm and use an html form tag with a Url.Action
<form action="#Url.Action("Times", "Patron")##Model.SelectedTimeSort" id="frmBuildings" method="post">
</form>
Relying on javascript per the correct answer seems like overkill just to keep the "BeginForm" around.

JSP - Loads of submit buttons and loads of IF statements

My JSP page has many Submit buttons for example
<input type="submit" name="RemoveCustomer" value="Submit"/>
<input type="submit" name="AddCustomer" value="Submit"/>
and within my scriptlet tag I have something like this
if(request.getParameter("addByName") != null && request.getParameter("addByName").length() > 0) {
...
} else {
if(request.getParameter("removeByName") != null && request.getParameter("removeByName").length() > 0) {
...
} else {
...
}
}
Where addByName and removeByName are HTML TextFields
Clearly I could click the button RemoveCustomer to access removeByName and do something, but if the addByName TextField was not empty it would add a customer.
What I want to know is in my scriptlet how do I know what submit button was pressed?
You could put sections in <div> with class or id that makes clear what has happened.
It sounds to me like your JSP is in need of a good refactoring. If you agree, I have two suggestions:
Eliminate all scriptlet code and use only JSTL. JSPs are for view only; move all logic out of the page.
The logic really belongs in controllers on the server side. Have a Front View Controller servlet that accepts all incoming traffic and delegates requests to classes that are written specifically to handle different use cases.
You might have grown to the point where your app could benefit from an MVC framework like Spring, Wicket, etc. I'd recommend looking into one.
You can do request.getParameter("name of submitbutton"). Here name of submit button is RemoveCustomer or AddCustomer.