1

I want to be able to pass the string output of my javascript function as a parameter to my MVC 4 Razor Url Action which itself is passed as a custom attribute to the input element. Here is the razor in my view file:

@Html.TextBoxFor(model => model.SomeProperty, new{data_autocomplete = @Url.Action("SomeAction","SomeController", new { someParameter= "here I want the string output of my javascript function" })})

How can I possibly achieve this?

2 Answers 2

1

I believe this could be done only after the HTML is rendered. The reason behind this is the Razor is mainly used for rendering the HTML. The Javascript could be run upon for a HTML element only its is completely rendered. If I understood your question, you want to dynamically assign the URL which is rendered by the javascript function to the data_autocomplete property of the textbox. I would suggest you the method below

var currentURL = @Url.Action("SomeAction","SomeController");

var parameters  = YourJSMethodToreturnTheParameterValue();

$("#SomeProperty").attr("data_autocomplete",currentURL+'?someParameter='+parameters;

Ideally, the TextBoxFor would render the Property name as its ID.

Sign up to request clarification or add additional context in comments.

2 Comments

Well, thanks for the reply. I did it but it seems the js added parameter isn't passed to the action. It is null when I break point it!
One point: Just ensure if the action has the parameter with same name and query string param? Can you please post your sample script and the action method ?
0

Try this:

var textbox = document.getElementById('SomeProperty');
var currentUrl = textbox.getAttribute('data_autocomplete');
currentUrl  += '?someParameter=' + myJsFunctionThatReturnsString();
textbox.setAttribute('data_autocomplete', currentUrl);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.