开发者

ASP.NET MVC Generic Partial View Pattern

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.
 开发者_如何学JAVA 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.


You could just have a simple action method that calls the partial for one model instance

public PartialViewResult Single(string partialName) {
    return PartialView(partialName);
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜