I'm working on reading a value from textBox (let it be Sam):
<%= Html.TextBox("Name")%>
and then on click of action link:
<%: Html.ActionLink("Edit","Edit",routeValues %>
I need to route (this URL should open) /Edit/Sam
How can I do that?
Since you aren't using any route values above and instead just the name of the textbox you can just create a link
Name your textbox "name" (if it isn't already) via the html attributes new {id="name"} (for ex)
then you can just jQuery to get the value and append it
Edit
You could also use the html help above and just attach an onclick event handler for jQuery as well.
$(document).ready(function() {
$("#name").click(function() {
window.location.href= $('#idOfLinkHref').attr('href') + '/' + $('#name').val()
});
});
something like that anyways off the top of my head.
There are a lot of ways to do this - these are just a couple ideas.
Related
In my ASP.net MVC Application. I am facing a problem and really struggling for it.
I have an edit form in a View , in that i have a label which is defined as:
<% using (Ajax.BeginForm("AjaxEdit", "Home", new AjaxOptions { OnSuccess = "Success", OnFailure = "Failed",UpdateTargetId = "ChangedOn"}))
{ %>
<%=Html.HiddenFor(Model => Model.ChangedOn)%>
<%:Html.DisplayFor(Model => Model.ChangedOn)%>
after Clicking on the submit buttton,the ajax request is called and server response is sent back.
But, the Problem is i was not able to update that hidden field with the server response recieved.
The problem is beacuse it is of type hidden input.
is there any other way of doing this..please help.
Try to use html tag for hidden input to see what will happens:
<input id="ChangedOnId" name="ChangedOn" type="hidden" value="<%=Model.ChangedOn%>">
If you want to change value of hidden input you can do this:
$("#ChangedOn").val(yourValue);
And if you want to change it's text change DisplayFor with a tag that you can select that.
I think DisplayFor only render a plain text of your property
Hidden fields are stored in ModelState
You can remove it
ModelState.Remove(nameof(Model.ChangedOn));
I'm looking for a way, how a user can type the ID of a record (i.e. /controller/23) , then press the goto button and it navigates directly to /controller/id
So the user is on the index page. There is something like a number_field_tag (input field), wrapped inside a form? You type the ID of the record and then click GOTO.
I don't want to explain to a user they can do this simply by changing the URL, so i prefer a solution that uses the page.
Preferable non javascript. Any ideas?
Thanks.
You'll either need to write another action to handle the translation of submission -> redirection to the URL cleanly or use a javascript solution in my opinion.
<script type="text/javascript">
$('form') // replace with a more detailed selector of your form (e.g. #formid)
.submit(function() {
window.location.href = 'http://foo.bar/controller/' + $('#field-id').val();
return false;
});
</script>
That bit of JS will accomplish what you need if you decided that a JS approach is satisfactory.
I think this might do
<%= link_to "GOTO" XXX_path(params[:id_of_field]), :class=>"STYLE_AS_BUTTON" %>
I'm working with ASP.NET MVC 2 and building a simple business app. Here are some of the details:
The app deals with work orders and
has a work order index view. The
view has a table listing the work
orders, and several controls (text
boxes, check boxes, and drop down
lists) to select the criteria for
which work orders to display.
I'm using viewmodels. The work order
index view has a viewmodel with
properties for each and every
control.
I've implemented paging similar to
what is being done in the answer to
this question:
How do I do pagination in ASP.NET MVC?
I'm using LINQ's Skip() and Take() as
demonstrated, and ActionLinks for the
navigation.
If I load the page and don't
manipulate any of the controls, I can
click on the page number ActionLinks
and move around just fine between
pages of work orders. However, if I
change something, my changes are lost
when I navigate to another page.
For example, if I'm on page 1 and
click an unchecked check box, and
then click on the link for page 2,
the second page of results will load
but the check box will revert to its
previous state.
I understand why this happens, but I'm wondering what is the best thing to do from a design standpoint.
Potential solutions I can think of:
Set all the control values as route
values in the ActionLinks. This
seems really nasty, and could result
in very long URLs or query strings. Actually, now that I think of it this wouldn't work without a way to capture the control values.
Since ActionLinks don't post
anything, replace them with buttons.
Again, this seems like a bad idea.
Change the ActionLinks to links that
fire off a jQuery script that does a
POST. I think this is the most
promising option so far. Do many
developers do it this way?
This seems like a common enough problem, but none of these options feel quite right. I wonder if I'm missing something.
Can't you just save the changes back to the database when the user toggles the checkboxes (using jQuery):
$("input[type=checkbox]").click(function() {
$.ajax({
type: "POST",
url: "/ControllerName/SaveInfo?id=" + {id},
success: function(){
alert("Data Saved: " + msg);
}
});
});
In the end, I wound up getting rid of the ActionLinks for the paging, and replaced them with regular anchor tags. The current page index is now stored in a hidden form value:
<input id="page" name="page" type="hidden" value="" />
<p>
<% for (var i = 1; i <= (int)Math.Ceiling(Model.RowsMatchingCriteria / (double)Model.PageSize); i++) { %>
<%--
If the page number link being rendered is the current page, don't add the href attribute.
That makes the link non-clickable.
--%>
<a class="pageLink" <%= i != Model.Page ? #"href=""javascript:void(0);""" : string.Empty %>><%: i %></a>
<% } %>
</p>
Then I added the following jQuery script, which sets the hidden page value and submits the form when a link is clicked:
$(document).ready(function () {
$('.pageLink:[href]').click(function () {
$('#page').val($(this).text()); // Set hidden field value to the text of the page link, which is the page number.
$('form:first').submit();
});
});
Problem solved.
Best bet is to effectively simulate viewstate by "logging" the changes to a hidden field when a user paginates. To do so:
1) Figure out what data you need to capture and a data format to do so in {ie -- an array of json objects}
2) Setup the link that handles the prev/next to fire off a method to collect the "changed" things and stuff them into objects and into a hidden field.
3) When posting the form, parse the hidden field, extract data and profit.
I have this ruby on rails code
<%= builder.select(:serving_size_id, '') %>
I have not specified any options on purpose because I set the options in a different way when the page loads (using jQuery and Ajax).
The question: Is there any way I can get the value from the column "serving_size_id" but not change that line? I have a partial which I use it for new and edit and I think it would be sweet if I can do the setting of the selected index in JS.
Any ideas?
I'm not sure I completely understand your question, but if you want to set the value of the select field with JavaScript, you need to obtain the value in JavaScript at some point. I can think of two ways of doing this:
1) When you get the options via AJAX, have the server indicate which one is selected. This can be done by returning HTML <option> tags with selected="selected" set for one of them. To do this, your AJAX request is going to have to provide information about the object this select field is for (so the server can look up the object's current serving_size_id value).
2) When you render the field in your original partial, also render some JavaScript which sets the current value of the field, for example, underneath what you have above:
<%= javascript_tag "var ssid = '#{builder.object.serving_size_id}';" %>
Then, after the options are retrived via AJAX, the ssid variable is checked and the correct option is selected.
using jQuery in rails is easy but a little more difficult than prototype.
ex: "div id="serving_size" class="nice" rel="<%=h num%>">Stuff Goes Here.../div>"
in application.js do the following:
//application.js
$(document).ready(function(){
if($('#serving_size'){
$('#serving_size').live("mouseover",function(){
//we are hovering over specific div id serving size
if($('#serving_size').hasAttr('rel')){
alert($('#serving_size').attr('rel'); //your dynamic rel value, and fire function
}
}
}
if('.nice'){
$('.nice').live("mouseover",function(){
//we are now hovering over any item on page with class nice
if($(this).hasAttr('rel')){
//we are now using jQuery object ref and finding if that obj has attr rel
alert($(this).attr('rel')); // shows dynamic rel value
}
}
}
});
If you use the above code you should be able to do anything you want and fire any custom code from each of your set event callbacks.
The 'live' function in jQuery is great because it can be called on items that will eventually be on the page (eg. if you fill in something with ajax, jQuery will be prepared for that item being in the page)
I hope this help.
I have a simple checkbox, generated with:
<%= Html.CheckBox("myCB" )%>
How can I add an onChange handler to this that does a submit?
Add an onClick handler to the CheckBox that submits the form the CheckBox belongs to...quick, clickHandler codeless example:
<%= Html.CheckBox("myCB",
new { onClick = "$(this).parent('form:first').submit();" });
(example definitely not checked for accuracy)
If you have only one form, and are not using JQuery (you should be, by the way) try this:
<%= Html.CheckBox("myCB",
new { onClick = "document.form.submit();" });
I would highly recommend using jQuery to support this because it makes it easier to add the behavior to a checkbox throughout your site by having the selector either be ID or class-based. Then you could put the script anywhere on the page or in an external .js file.
<script language="javascript" type="text/javascript">
$('#myCB').click(function() { $(this).parent('form:first').submit(); });
</script>
Alternatively, the selector could be class-based (or any attribute for that matter). More info here: http://docs.jquery.com/Selectors