2014-11-08 21 views
3

我有一个文本框来选择日期,并且在剃刀页面中有一个动作链接重定向到另一个带日期值的页面作为参数之一。 有没有什么办法可以在用户更改日期时动态更改actionlink的值。 我的Razor视图就像更改来自Javascript的动作链接参数

<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"}) 

而在Javascript中我已经能够做到

$("#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 ? 
} 

有没有办法做到这一点?

+0

请告诉我操作方法(参数名称)的签名? – 2014-11-08 04:37:29

+0

约会出席/添加?id = model.id&date = 2014-11-08 05:06:52

回答

2

,您可以更新使用

var url = $('#mylink').attr('href'); // returns "Attendance/Add/1" 
function dateChanged(ev) { 
    $('#mylink').attr('href', url + '?date=' + <mydatevalue>); 
} 

注意到这一假设你给的链接一个id的链接href属性

@Html.ActionLink("Mark Attendance", "Add", "Attendance", new { id = @Model.ClassId }, new { @class = " btn btn-primary", new id = "mylink" }) 

这将重定向到

public ActionResult Add(int ID, DateTime date) 
3

您可以更改使用javascript的日期值

$('.btn btn-primary').attr('href', function() { 
     return this.href.replace('_Parameter1', Date_Value); 
      }); 

在View: -

@Html.ActionLink("Mark Attendance", "Add", "Attendance", new { @id = @model.ClassId,Parameter1='_Parameter1'}, new { @class = " btn btn-primary"}) 

在控制器: -

public ActionResult Add(int ID, DateTime date)