Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello


I have dropdownlist

@Html.DropDownListFor(model => model.EmpName, new SelectList(ViewBag.names, "Text", "Value"), "Select Name", new { @class = "form-control", @id = "drop" })


and i want to set it's value as parameter in ajax action link

@Ajax.ActionLink("Make month salary", "createsalary", new { @name = wanna put value here }, new AjaxOptions()
{
    UpdateTargetId = "Sal",
    HttpMethod = "Get",
    InsertionMode = InsertionMode.InsertAfter
});




What i should to do

What I have tried:

i tried to use javascript to retrive data in textbox or label but still don't know how i convert it to parameter
Posted
Updated 16-Apr-20 8:03am
Comments
F-ES Sitecore 16-Apr-20 11:04am    
You can't because the server code (in an "@" block) runs on the server and passes the html to the client, so when your ActionLink is executing the dropdown doesn't yet exist. You'll need to add the param yourself via javascript, probably when the dropdown changes. Google for "javascript update link on dropdown change" for examples on how to do this.
Member 13058758 16-Apr-20 14:02pm    
thank You it solve my problem

If you've enabled the "unobtrusive AJAX" option, something like this should work:
Razor
@Ajax.ActionLink("Make month salary", "createsalary", new { @name = wanna put value here }, new AjaxOptions()
{
    UpdateTargetId = "Sal",
    HttpMethod = "Get",
    InsertionMode = InsertionMode.InsertAfter,
    OnBegin = "onCreateSalaryBegin"
});

...

<script>
function onCreateSalaryBegin(xhr, settings) {
    var empName = $("#drop").val();
    settings.data = $.extend(settings.data, { EmpName: empName });
}
</script>

To enable unobtrustive AJAX, you'll need an appSetting in your web.config file:
XML
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
You'll also need to include the jquery.unobtrusive-ajax.min.js script in your view.
 
Share this answer
 
Comments
Member 13058758 16-Apr-20 11:18am    
it retrieve null value
Richard Deeming 16-Apr-20 11:38am    
What receives a null value? The parameter in the action? The Javascript function? Something else?

Try using the developer tools in your browser to examine the AJAX request to see what's being passed to the server.
Member 13058758 16-Apr-20 12:02pm    
on click on link Action it didn't retrieve any data or parameter
Richard Deeming 16-Apr-20 12:03pm    
So use the developer tools in your browser to examine the AJAX request.
Member 13058758 16-Apr-20 14:02pm    
thank you i found another solution
document.getElementById('drop').onchange = function () {
    var path = document.getElementById('anchorclick').href;
    var Selectedvalue = $('[id*=drop] option:selected').text();
    path = path.replace("PaymentCodeVal", Selectedvalue);
    document.getElementById("anchorclick").href = path;

};


i solve it by java script function
 
Share this answer
 
Comments
Richard Deeming 16-Apr-20 14:18pm    
That's only going to work once. If you change the dropdown list twice, the link will still be referring to the first selected value.
Member 13058758 17-Apr-20 3:54am    
client can't make another choice this link go to another page after choice

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900