Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

any thoughts on this?

I'm using MVC2 in VS2010. I have javascript in my view to put markers on a google map. I need to get the address and distance from text boxes,

<%= Html.TextBox("address") %> <%= Html.TextBox("distance") %>

to use as arguments in the function drawMap(address, distance); in my code.

Is there a register object I can use to access this? Also, having some issues using <%= Html. (..) %> in the <script type="text/javascript"><script> flags. I realise I should probably be implementing the MVC model here, and although use this to populate the textboxes on loading, can't pull the information from a textbox back out again on the POST back, to get values in the javascript function in the view.

Any suggestions greatly received! Thanks in advance.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // further question re: DropDownListFor<>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Is there also a simple way of getting information from:

<%: Html.DropDownListFor(model => model.SavedAddress1, ViewData["StartLoc"] as SelectList) %>

The SelectList for this is held in the Controller. Would I get which one had been selected from the controller in the POST? Or is there a way of determining which had been selected directly in the Javascript.

Many thanks

share|improve this question
    
post some code please.. from view, from js –  Samich Sep 10 '11 at 15:05
add comment

1 Answer 1

To get the textbox value just use javascript

document.getElementById("address").value 

or with jquery use

$("#address").val();

If you instead use a model passed to the view (which I prefer if user don't have to change the values in the textbox) you can put that in the script

<script type="text/javascript">
drawmap('<%= Model.Address %>', '<%= Model.Destination%>');
<script>

To have this value persisted in the form post you can use the Html.HiddenFor tag

 <%: Html.HiddenFor(model => model.Address) %>

Update to your edit

For the dropdownlist if you put it in the form tag, it will be bind to the model, so you will get in the controller. While with javascript you can access the value the same way yuo use for the textbox

share|improve this answer
    
The jquery part should be $("#address").val(); –  malik Sep 10 '11 at 15:21
    
ops. you're right. Updated and thanks for pointing out –  Iridio Sep 10 '11 at 15:23
    
Thank you Iridio, the getElement() to great. –  sald Sep 10 '11 at 15:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.