我想int
在点击时将模型和传递给控制器ActionLink
。
@Html.ActionLink("Next", "Lookup", "User", new { m = Model.UserLookupViewModel, page = Model.UserLookupViewModel.curPage }, null)
不起作用,而是传递了模型的空白实例,使用时会期望该实例new
。
@Html.ActionLink("Next", "Lookup", "User", Model.UserLookupViewModel, null)
确实有效。
控制者
[HttpGet]
public ActionResult Lookup(UserLookupViewModel m, int page = 0)
{
return this.DoLookup(m, page);
}
查看模型
public class UserLookupViewModel
{
public int curPage { get; set; }
[Display(Name = "Forenames")]
public string Forenames { get; set; }
[Display(Name = "Surname")]
public string Surname { get; set; }
[Display(Name = "DOB")]
public DateTime? DoB { get; set; }
[Display(Name = "Post code")]
public string Postcode { get; set; }
}
如何将它们一起传递?该Lookup
方法的参数匹配在ActionLink的命名属性。
您不能使用ActionLink通过Link传递属性,但是可以执行以下操作来获得相同的行为。
<form action="/url/to/action" Method="GET">
<input type="hidden" name="Property" value="hello,world" />
<button type="submit">Go To User</button>
</form>
如果您创建一个帮助程序来生成这些GET表单,则可以像设置常规链接按钮一样对它们进行样式设置。我唯一要注意的是,页面上的所有表单都易于修改,因此我不信任数据。我宁愿在到达目的地时再次提取数据。
在创建搜索动作时,我使用上述技术,并且希望保留搜索历史并保持后退按钮正常工作。
希望这可以帮助,
哈立德:)
聚苯乙烯
起作用的原因。
@Html.ActionLink("Next", "Lookup", "User", Model.UserLookupViewModel, null)
是因为ActionLink方法的参数列表被概括为采用对象,所以它将采用任何对象。它对该对象的作用是将其传递给RouteValueDictionary,然后尝试根据该对象的属性创建查询字符串。
如果您说该方法可以正常工作,则还可以尝试向视图模型添加一个名为Id的新属性,该属性将按您希望的方式工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句