user1204195
user1204195

Reputation: 503

How to get value entered in a text box from a view

My MVC view has a table with an editable field as follows.

  <td>
     <div class="view">
         <%= Model.device_Url%>
         </div>
         <div class="edit">
         <input type="text" name="deviceurl" id = "deviceurl" value="<%= Model.device_Url%>" />   

      </div>
 </td>

The user has the option to edit then save the value entered in the above text box field.

 <td class="options">
    <div class="view">
        <a href="#" class="switchToEdit">Edit</a>
    </div>
     <div class="edit">
      <a href="/Live/Update?streamurl=deviceurl class="save refresh">Save</a>
      <a href="#" class="cancel">Cancel</a>  
        </div>
    </td>

I would like to call my controller /Live/Update and pass in the value changed in the text box in the URL.

How would I get the value entered inthe text box.

Do I need java script to do this?

Upvotes: 2

Views: 426

Answers (2)

HatSoft
HatSoft

Reputation: 11191

My advise will for you to use Html.ActionLink instead of using this Save

like this : 
<%=Html.ActionLink("Save", 
                "Live",  // <-- Controller Name.
                "Update",   // <-- ActionMethod
                new { streamurl = deviceurl },
                new { @class = "save refresh" }
                )
%>

Upvotes: 0

Nick Harrison
Nick Harrison

Reputation: 937

You will need javascript unless you want to use a traditional submit.

The easiest approach would be to use jQuery and get the value with a line of code similar to this:

$("#deviceurl").val()

I would then make an ajax call to submit the value back to your controller's action. You also probably want to use the Url.Action helper to build the Url:

$.ajax(
        url: '<%=Url.Action("Update")%>,
        data : {deviceUrl :  $("#deviceurl").val()},
        success : function (result){
           // handle logic when the update succeeds
        }
      );

Hope this helps

Upvotes: 1

Related Questions