vadrians
vadrians

Reputation: 3

MVC - passing values from javascript to View render

I hope you can help me with this.

I need to do the following but I dont know if its possible.

On my view I have a function that receives one parameter. That value already exists in a texbox field but I don't know how to send it to my function.

 <% Permissions TV = new Permissions();  %>            
 <%: Html.TreeViewAsUnorderedList(TV.GetPermissionsItems((int?)document.getElementById('txtPerfil').value  ))%>  

 <%: Html.TextBox("txtPerfil","" , new { disabled = "true", style = "width:20px", id = "txtPerfil"  })%>

Regards,

Upvotes: 0

Views: 1281

Answers (2)

Yasser Shaikh
Yasser Shaikh

Reputation: 47774

Ok, you have a value in a Textbox which you want to send to server side (function/action).

You should have a event, on which you can send this value to your action method. Events can be any simple javascript event like, onBlur, onClick etc... that I leave on to you to decide.

Now sending value to function. Here if you dont want a post back to happen, which I assume you do, you need to make an ajax/json call to your action and pass the value here.

Sample Code : taken from this article

Js/Jquery Code

var url = '@Url.Action("GetData")';
$.ajax({
url: url,
type: 'GET',
cache: false,
data: { value: strId},

success: function (result) {
$('#result).html(result);
  alert(result.foo + result.ball);
}
});

CSharp Code : Action Method (returning a Json)

public ActionResult GetData(string value)
{
return Json(new {foo="bar", ball="dragon"});
}

Hope this helps.

Upvotes: 2

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

Your function is a server code, but javascript is a client code, so it is not possible in that way. If you give us more information, we give you advice how to do what you need

Upvotes: 2

Related Questions