Yash Dutt
Yash Dutt

Reputation: 197

asp.net mvc razor view - redirect based on the value of a form control

I have the following anchor tag in MVC 5 razor view:

<a href='@string.Format("https://www.abc.ac.fj/index.php?id=10820&Full_Name={0}&[email protected]", name)' target="_blank">Pay Now</a>

and a form control for the EmailContact property

@Html.TextBoxFor(m => m.EmailContact)

The email address, I am trying to use from the Model property but it's not working. When you click this link, the value that gets rendered is "@Model.EmailContact" but not it's actual value.

Please assist.

Upvotes: 1

Views: 232

Answers (1)

user3559349
user3559349

Reputation:

You format the href value, you would need to use

<a href='@string.Format("https://www.abc.ac.fj/index.php?id=10820&Full_Name={0}&Email_Address={1}", name, Model.EmailContact)' target="_blank">Pay Now</a>

however @string.Format() is razor code which is evaluated on the server before its passed to the view, so it will be the initial value of EmailContact and does not take into account a value edited in the textbox.

You need to build the url using javascript/jquery. Change the link to

<a id="pay" href="#" data-baseurl='@string.Format("https://www.abc.ac.fj/index.php?id=10820&Full_Name={0}", name)'>Pay Now</a>

and add the following script

$('#pay').click(function() {
    var baseUrl = $(this).data('baseurl');
    var url = baseUrl + '&Email_Address=' + $('#EmailContact').val();
    window.open(url, '_blank')
});

Upvotes: 1

Related Questions