我有一个模板通过foreach: viewModel.foos
. 在这个模板上,我想做这样的事情:@Html.RouteLink("View", "Foo", new { id = fooId, text = fooName })
。视图模型的存在fooId
和fooName
属性。
2 回答
我已将此添加到我的模板中:
<a data-bind="attr: { href: Url }">View</a>
这对我的 Foo 对象:
public class Foo : FooBase {
public long FooId { get; set; }
public string FooName { get; set; }
public string Url {
get {
return string.Format("/foo/{0}/{1}", FooId, FooName
}
}
}
缺点:
- 痛苦的可扩展性。
优点:
- 简单。
This does not answer question, but I will keep it here because it shows how you can do it for a more SPA like approuch.
If you are looking to move navigation to client check out Durandal or my light weight SPA engine https://github.com/AndersMalmgren/Knockout.Bootstrap.Demo
Old answer before question was updated
You have to do this from your model (Which is a good thing because it should not be done in the view)
Let your subview model take the parameters as constructor arguments
MyViewModel = function() {
this.subView = ko.observable();
};
MyViewModel.prototype = {
loadSubView: function() {
this.subView(new SubViewModel(this.params));
}
};
edit: Tip test this lib for templating http://jsfiddle.net/hwTup/