Change Actionlink parameter from Javascript
I have a text box for a date picker and the razor page has one action link to redirect to another page with a date value as one of the parameters. Is there a way to dynamically change the value of the actionlink whenever the user changes the date. My razor look is like
<div class="col-md-4">
@Html.TextBoxFor(model => model.Date, new { @class = "datepicker form-control" })
@Html.ValidationMessageFor(model => model.Date)
</div>
@Html.ActionLink("Mark Attendance", "Add", "Attendance", new { @id = @model.ClassId }, new { @class = " btn btn-primary"})
And in Javascript I was able to do
$("#Date").datepicker({
showOn: 'both',
altFormat: 'MM-dd-yy',
dateFormat: 'MM-dd-yy',
changeMonth: true,
changeYear: true,
yearRange: "1900:2050"
})
.change(dateChanged)
.on('changeDate', dateChanged);
function dateChanged(ev) {
//How can i change the action link in a way that add an extra param date with selected value ?
}
Is there a way to do this?
+3
source to share
2 answers
You can change date value with javascript
$('.btn btn-primary').attr('href', function () {
return this.href.replace('_Parameter1', Date_Value);
});
In view: -
@Html.ActionLink("Mark Attendance", "Add", "Attendance", new { @id = @model.ClassId,Parameter1='_Parameter1'}, new { @class = " btn btn-primary"})
In the controller: -
public ActionResult Add(int ID, DateTime date)
+3
source to share
You can update the href attribute of links using
var url = $('#mylink').attr('href'); // returns "Attendance/Add/1"
function dateChanged(ev) {
$('#mylink').attr('href', url + '?date=' + <mydatevalue>);
}
Note that it is assumed that you provide a link id
@Html.ActionLink("Mark Attendance", "Add", "Attendance", new { id = @Model.ClassId }, new { @class = " btn btn-primary", new id = "mylink" })
which will be redirected to
public ActionResult Add(int ID, DateTime date)
+2
source to share