我有一个像这样的 MVC ActionLink(效果很好)
<%: Html.ActionLink(user.UserName, "Details", "Users", New With{.id = user.ID, .slug = Replace(user.UserName," ","-")}, nothing)%>
但由于不“推荐”在视图中进行字符串操作,我想知道如何构建自定义 Html ActionLink 来为我进行字符串替换?
我有一个像这样的 MVC ActionLink(效果很好)
<%: Html.ActionLink(user.UserName, "Details", "Users", New With{.id = user.ID, .slug = Replace(user.UserName," ","-")}, nothing)%>
但由于不“推荐”在视图中进行字符串操作,我想知道如何构建自定义 Html ActionLink 来为我进行字符串替换?
自定义 ActionLink 似乎也是错误的地方,最好通过自定义视图模型将 Slug 从控制器传递到视图。Slug 可以是 View Model 上的一个属性,以及在 setter 中调用的字符串逻辑。
例如,将 UserViewModel 类添加到“ViewModels”文件夹。
public class UserViewModel
{
public User User { get; private set; }
public string Slug { get; private set; }
public UserViewModel(User user)
{
Slug = Replace(user.UserName," ","-");
}
}
然后在控制器中,将其传递给视图:
return View(new UserViewModel(user))
有关 ViewModel 用法的更多信息: