Asp.net MVC add textbox value to routelink - asp.net-mvc

I am trying to learn asp.net MVC and having an issue to submit a textbox value to a model.
I have a text box in which users will type a number and when they hit the routelink the routelink will take the value from the textbox and assigns it to one of .Page item.
<%=Html.TextBox("PageIndex")%>
<%=Html.RouteLink("Search", "UpcomingEvent",
New With {.Page = "I want to put value of PageIndex textbox here"})%>
How can I assign the value of the textbox to .Page variable?
Thanks for your time and help!

You can't do that because the RouteLink gets rendered on the server.
If you want to construct a URL based on the user input without a postback, you'll need to do some client-side scripting (ie JavaScript).

It sounds like you're not expecting to post back to the server once they have the textbox value entered. If that is the case then you're going to need to use javascript to change the link's href property. Html.RouteLink is all done server side.
If you are using jquery then it would be something like
$("#pageIndex").change(function()
{
$("#pageLink").href += "?pageIndex=" + $("#pageIndex")"
}
Of course that isn't going to work with multiple change events being fired but that part is left as an exercise for the reader.

Related

Getting value from a text field when button is clicked using aspnet mvc Razor in ext.net

I'm having some issues referencing a component to get its value. I have a simple form with a numberfield and a button. To test I just want to output the value of the numberfield when the button is clicked
I have a basic js function
var saveNewSats = function (component) {
console.log(component.getValue());
};
In razor I wire it together like this
X.Button()
.Text("Save")
.Listeners(l => l.Click.Handler = "saveNewSats(#{newSatsField});")
Where newSatsField is the html id of the numberfield. I tried to reference the field in multiple ways like App.newSatsField, but no luck. What is the correct syntax to get the value from the numberfield?
Best regards
So I found the solution.
I did set the InputId which renders the html id attribute. What I needed was to set the ID as this is the one referenced by ext.net. Now both the #{id} and Ext.Cmp syntax works as expected.

Asp.Net MVC Model Binding with JQWidgets

When you want to create a DateTime picker control with JQWidgets, you must define a div element and then call a function like this using Javascript:
$("#MyDivElementId").jqxDateTimeInput().
The problem is: I'm not able to figure out how I can use Model Binding of Asp.Net MVC with this syntax. I mean, the Model Binding feature will try to match key-value pair received from input controls in the form element and obviously, div element are not input control.
I found somebody who already resolved this problem using hidden field set with values of matching div JQWidgets element before submitting form but I don't like this solution; it's not natural and I must write to much code for a thing that should be simpler in my view.
Does anybody have more elegant solution?
If you set the "name" attribute of the DIV tag, the value from the DateTimeInput's Input tag would be submitted.
First of all when you submit id is not submited and i just opened that plugin demo. when you add code $("#MyDivElementId").jqxDateTimeInput(). it will create textarea with name MyDivElementId and when you submit then you will have the same value on server side. Other issue can be with date format since they would be probably different on client side and server side.
try to add input parameter for controller "DateTime MyDivElementId" and check if its null or not.

ASP.Net MVC Postback does not clear textbox

On a .cshtml page I have a single textbox inside a form. When I enter some text and press return it triggers a POST to a controller of the same name (different signature). The code processes the text and a model is created and passed back to the same page where in addition to the original textbox a grid is now populated (conditionally). Everything works, except the text in the textbox is retained.
This is a little odd, considering that most of the questions on here are about retaining the text after a POST and the indication has been that the text should not automatically be retained.
I can probably assign the textbox an empty string but I am simply wondering if I have this wrong?
Even if it was a simple textbox, html textbox is a input control that has two very important properties, i.e.
name
value
browser will send this name value pair to server
and the default behavior of server is to return the collection of all pairs back to browser when its done whatever it was supposed to do.
this collection is called post data. (or sometimes form data)
Yes, you could clear the textbox in code if everything was fine on postback
If something was wrong (exception or validation) you could do nothing ( as you do now) and text will be there again, what's makes sense in the UI
.

"Next" Button disabled until values are selected and typed in DropDownLists and Textboxes in MVC?

I am using MVC-Viewmodel, EF model first on my project.
I have 3 DropDonLista and a few TextBoxes in my View, User can select Values in the DropDownLists and Type inside the TextBoxes. I want that my "Next" button is disabled until values are selected and textboxes are filled then it gets enabled.
How can I easiest way accomplish this?
I've done this kind of things with C# Winforms and its pretty easy but in MVC I have no clue how I can do this.
Thanks in Advance!
You would need to use a client side scripting language like JavaScript. JQuery (a framework to make JavaScript easier to use) is now integrated in to MVC3+, so implementing it is much easier than it has been in the past.
You can target HTML DOM elements (HTML tags in your page, in layman terms) in jquery using "selectors" - i.e. if you want to access a HTML textbox called "test" in your form, and check the value, you can do the following:
var value = $("#test").val();
if(value == '') {
// do something
}
JavaScript syntax is strikingly similar to C#, but it works on the client side (it's processed by the browser), rather than the server.
you can use javascript/jquery to check if values are selected and textboxes are filled then enable the next button.

ASP .NET MVC: Html.Radiobutton onclick -- set ViewData[""]

I'm new to ASP .NET MVC and to web programming in general.
I'm wondering if there is a way to set ViewData variables when a radiobutton is selected -- but before the page is submitted.
Maybe I'm barking up the wrong tree but what I'm trying to do is create a form where new fields are added based on which radio button is selected. So what I want to do is when a radiobutton is clicked it sets a ViewData variable and based on that ViewData variable a different partial view loads the appropriate fields below the current field.
I imagine there must be someway of doing a onclick="some C# function that sets ViewData(args)"
Thanks
There are a couple of ways you could go about this.
1) You could have an Ajax form where through Javascript you post the form back and check to see if it's an Ajax Request, there by returning a partial view to a div that you specify.
2) Post the form as is and check server-side to see if the radio button was clicked, and thus redisplay the form with the new options visible.
If you take the first approach it would be easy enough to fall through to the second one for those without Javascript enabled.
There aren't really "onclick" events as I'm assuming you are used to from Webforms, you would basically have to roll your own Javascript to handle such things. Once you do a few, I think you'll find it's really not too bad, with the benefit that you'll have more control over what you're doing and through that gain a better understanding of the larger picture.
ViewData only exists, and only exists server-side, for the lifetime of the request. So, once the page is rendered the object no longer exists.
Some alternate approaches you can take:
1 - Use client-side Javascript to add a form and inputs as necessary. More info here:
ASP.NET MVC & JQuery Dynamic Form Content
2 - Pre-render the new form, but hide it via CSS, and unhide it when the appropriate radio button is clicked. More info here:
expand collapse html field Firefox
3 - Use AJAX to render the new form when the appropriate radio button is clicked. More info here:
http://www.asp.net/learn/mvc/tutorial-32-cs.aspx

Resources