ASP.NET MVC 通用部分视图模式
我有一个片面的观点,我想成为通用的。根据这个问题,部分观点不能是通用的。因此,我制作了一个 HtmlHelper 扩展来处理我想要类型安全的部分,然后将其余部分交给真正的部分视图。
通常我的助手会在页面加载时调用,效果很好,但有时我想通过 AJAX 添加一行或其他内容。发生这种情况时,控制器无法使用我的“部分视图”,因为它无权访问 HtmlHelper。
除了拥有 object
类型模型的部分视图之外,我还能做什么?
我正在使用 Razor,如果这很重要的话。
我正在做的事情的简化版本:
public static MvcHtmlString DoStuff<T>(this HtmlHelper html, IEnumerable<T> data,
Func<T, ViewModelType> StronglyTypedFn, string PartialName)
{
// the pre- and post-processing for the partial view is complex enough I'd like
// to encapsulate it. But I want the encapsulation to include the safety
// benefits that generics give.
var mappedData = data.Select(StronglyTypedFn);
string htmlData = "";
foreach(var model in mappedData){
htmlData += html.Partial(PartialName, model);
}
htmlData += "some boilerplate footer html";
return htmlData;
}
我意识到在这个例子中,我在部分视图之外只有很少的代码行,因此拥有一个助手似乎毫无意义,但在我的真实示例中它更加复杂。
现在,在 ajax 调用中我想返回 Html.DoStuff()
。但我不能,因为这需要访问 HtmlHelper,并且该助手在控制器内不可用。
I have a partial view that I want to be generic. According to this question, partial views cannot be generic. So I instead made an HtmlHelper extension that handles the pieces for which I want type-safety, then hands off the rest to a real partial view.
Usually my helper is called on page load, which works fine, but sometimes I want to add a row or something through AJAX. When this happens, the controller cannot use my "partial view" since it does not have access to the HtmlHelper.
Apart from having a partial view with a model of type object
, is there anything I can do?
I'm using Razor, if that is important.
A simplified version of what I'm doing:
public static MvcHtmlString DoStuff<T>(this HtmlHelper html, IEnumerable<T> data,
Func<T, ViewModelType> StronglyTypedFn, string PartialName)
{
// the pre- and post-processing for the partial view is complex enough I'd like
// to encapsulate it. But I want the encapsulation to include the safety
// benefits that generics give.
var mappedData = data.Select(StronglyTypedFn);
string htmlData = "";
foreach(var model in mappedData){
htmlData += html.Partial(PartialName, model);
}
htmlData += "some boilerplate footer html";
return htmlData;
}
I realize that in this example I have so few lines of code outside the partial view that it seems pointless to have a helper, but in my real example it is more complex.
Now, in an ajax call I want to return Html.DoStuff()
. But I can't, because this requires access to the HtmlHelper, and the helper isn't available inside a controller.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以只使用一个简单的操作方法来调用一个模型实例的部分方法
You could just have a simple action method that calls the partial for one model instance
您可以使用动态类型的视图而不是对象。
但是......这里似乎存在一些误解,因为控制器根本不应该尝试渲染视图。你能发布控制器代码吗?
在我看来,更好的选择是为 ajax 请求返回 JsonResult 并使用 JS 在客户端添加行。
You could use a View with a Dynamic type instead of object.
But... It seems as if there's some misunderstanding here because the Controller shouldn't try to render the view at all. Could you post the Controller code?
The better option is, IMO, returning a JsonResult for your ajax request and adding the row/rows on client side using JS.