How to get access to FormCollection, ViewData, and ModelState - asp.net-mvc

Is there any way to get access to the current running request's FormCollection, ViewData, ModelState, etc. when running in an ASP.NET MVC application other than if you are directly working in the View? I'd like to be able to call some custom handlers from within the view, but access these collections without having to pass them. I'm thinking something similar to HttpContext.Current in webforms?

Try,
var wrapper=new HttpContextWrapper(System.Web.HttpContext.Current);
var routeData = RouteTable.Routes.GetRouteData(wrapper);
Controller con = (Controller)ControllerBuilder.Current.GetControllerFactory().CreateController(new RequestContext(wrapper, routeData), routeData.Values["controller"].ToString());
var viewData = con.ViewData;
var modelState= con.ModelState;
var form=new FormCollection();
var controllerContext = new ControllerContext(wrapper, routeData, con);
Predicate<string> propertyFilter = propertyName => new BindAttribute().IsPropertyAllowed(propertyName);
IModelBinder binder = Binders.GetBinder(typeof(FormCollection));
ModelBindingContext bindingContext = new ModelBindingContext()
{
ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => form, typeof(FormCollection)),
ModelName = "form",
ModelState = modelState,
PropertyFilter = propertyFilter,
ValueProvider = ValueProviderFactories.Factories.GetValueProvider(controllerContext)
};
form = (FormCollection)binder.BindModel(controllerContext, bindingContext);

There is a ViewContext object that lets you link back to most of what you're asking for, but you really have to ask yourself why you're doing all of this in the view. (IMHO anyway)
Edit: I may have misread your question. There is a ControllerContext in the controller and a ViewContext in the view. Most of the extensibility points in MVC have some sort of Context object that lets you get at the Request and it's data.

Related

Initializing ViewData property inside ViewResult object

When I access ViewData inside a method in the controller, I am able to assign the value in the form of dictionary ie.
ViewData["message"]="this is a custom message";
but I got into a scenario where I was trying to handle the Exception in MVC, here is my code:
public void OnException(ExceptionContext filterContext)
{
if (!filterContext.ExceptionHandled && (filterContext.Exception is ArgumentOutOfRangeException))
{
filterContext.Result = new ViewResult { ViewName = "Error", ViewData = };
filterContext.ExceptionHandled = true;
}
}
Now when handling exception i would like to pass a message to the error page, so tried to access the Result property of the ExceptionContext.
Now
my question is why am I not able to assign a value to the ViewData in
a dictionary-like a format here
filterContext.Result = new ViewResult { ViewName = "Error", ViewData = };
This is also a property returning a ViewDataDictionary object, when am I able to assign a value in the Controller method like this ViewData["message"] = "argument exception error"; the why am I not able to do the same inside the ViewResult object.
I tried it myself and got an understanding on the inner workings of the MVC frameWork, please correct me if I am wrong and please provide an explanation for it, which would make to learn more about the framework.
When I access ViewData inside a method in the controller, I am able to
assign the value in the form of dictionary
This is because when we call the controller and the method, MVC takes responsibilty to assign objects to all the properties, thats the reason we could assign value for the ViewData inside the method.
filterContext.Result = new ViewResult { ViewName = "Error", ViewData =
};
When we are dealing with the ViewData property of the ViewResult class, we cannot assign the value, in this way ViewData["key"]="some value" because it requires ViewDataDictionary object. however we can do this to assign the value like this
var d = new ViewDataDictionary();
d["key"] = "some value";
filterContext.Result = new ViewResult { ViewName = "Error",ViewData=d };

Fake HttpContext? (ASP.NET MVC)

I am trying to render a partial view from a server side event (no request).
I found the following code which was able to render partial view without a controller.
Render MVC PartialView into SignalR response
public static string RenderPartialView(string controllerName, string partialView, object model)
{
var context = new HttpContextWrapper(System.Web.HttpContext.Current) as HttpContextBase;
var routes = new System.Web.Routing.RouteData();
routes.Values.Add("controller", controllerName);
var requestContext = new RequestContext(context, routes);
string requiredString = requestContext.RouteData.GetRequiredString("controller");
var controllerFactory = ControllerBuilder.Current.GetControllerFactory();
var controller = controllerFactory.CreateController(requestContext, requiredString) as ControllerBase;
controller.ControllerContext = new ControllerContext(context, routes, controller);
var ViewData = new ViewDataDictionary();
var TempData = new TempDataDictionary();
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, partialView);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
This works fine when used after a client request (SignalR/websocket), but if the code is triggered by a server-side event (WCF connection to another app), HttpContext.Current = null which causes an error.
How can I make this work?
PS. I have also tried Razor Engine but I'm getting the error "Unable to compile template. The name 'Html' does not exist in the current context". Since the template also contains "#Html", it seems that the razor engine cannot parse the template outside a controller.
I was able to make it work using RazorEngine by removing all #Html tags from the partial view file.
string template = System.IO.File.ReadAllText(path);
string partialView = RazorEngine.Razor.Parse(template, model, "cachename");
http://forums.asp.net/t/1923611.aspx/2/10?Fake+HttpContext+ASP+NET+MVC+#5459457

Using a custom ModelMetadataProvider in a unit test

Pretty new to MVC so hopefully this is a simple question.
I have written a custom binding attribute that requires access to the httpContext. In order to inject a mock httpContext during unit tests, I have written an InjectingMetadataProvider that populates the Context property on any of my custom attributes.
I have managed to get this to work in the following test:
[TestMethod]
public void Marker_ShouldBind_Id()
{
// Arrange
var formCollection = new NameValueCollection
{
{ "id", "2" }
};
var context = new Mock<HttpContextBase>();
context.Setup(c => c.User).Returns((IPrincipal)null);
var metaProvider = new InjectingMetadataProvider(context.Object);
ModelMetadataProviders.Current = metaProvider; //why do I need this?
var bindingContext = new ModelBindingContext
{
ModelName = string.Empty,
ValueProvider = new NameValueCollectionValueProvider(formCollection, null),
ModelMetadata = metaProvider.GetMetadataForType(null, typeof(Marker)),
};
var binder = new DefaultModelBinder();
// Act
var marker = (Marker)binder.BindModel(new ControllerContext(), bindingContext);
// Assert
marker.Id.Should().Be(2);
}
However, if I comment out the line that sets my InjectingMetadataProvider to ModelMetadataProviders.Current, then my InjectingMetadataProvider.CreateMetadata() override gets handed a blank list of attributes, and so the test fails because my custom attributes don't get their context set.
Why do I need to set it to Current when I'm using it explicitly anyway? I don't want to be setting static stuff in my tests.
I may be doing something stupid because I'm feeling in the dark a bit at the moment due to my unfamiliarity with the framework.
Inside the DefaultModelBinder, a new binding context is created when calling BindComplexElementalModel. Notice that it gets the metadata from the ModelMetadataProviders.Current, and not your custom model metadata provider.
internal ModelBindingContext CreateComplexElementalModelBindingContext(ControllerContext controllerContext, ModelBindingContext bindingContext, object model) {
BindAttribute bindAttr = (BindAttribute)GetTypeDescriptor(controllerContext, bindingContext).GetAttributes()[typeof(BindAttribute)];
Predicate<string> newPropertyFilter = (bindAttr != null)
? propertyName => bindAttr.IsPropertyAllowed(propertyName) && bindingContext.PropertyFilter(propertyName)
: bindingContext.PropertyFilter;
ModelBindingContext newBindingContext = new ModelBindingContext() {
ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, bindingContext.ModelType),
ModelName = bindingContext.ModelName,
ModelState = bindingContext.ModelState,
PropertyFilter = newPropertyFilter,
ValueProvider = bindingContext.ValueProvider
};
return newBindingContext;
}

Render Partial to String in the Controller or elsewhere

So basically I have a partial view which can build a nice table for me. I would like to email this table out every week to my users. Instead of having to basically copy the template again, I would like to forward my model to the controller and receive the corresponding generated HTML as a String.
Is it possible to do this in a Controller, I feel it should be a pretty simple process.
Put this into a Helper file:
public static string RenderViewToString(ControllerContext context, string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = context.RouteData.GetRequiredString("action");
ViewDataDictionary viewData = new ViewDataDictionary(model);
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(context, viewName);
ViewContext viewContext = new ViewContext(context, viewResult.View, viewData, new TempDataDictionary(), sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
And then from the controller you can call it like this:
var order = orderService.GetOrder(id);
var orderPrint = MyHelper.RenderViewToString(this.ControllerContext, "_OrderView", order);
If you search for rendering partial views to strings you'll also come across some good leads. That's what I did to come up with the following extension method for the ControllerBase class:
public static string RenderPartialViewToString( this ControllerBase controller, string partialPath, ViewDataDictionary viewData = null )
{
if( string.IsNullOrEmpty(partialPath) )
partialPath = controller.ControllerContext.RouteData.GetRequiredString("action");
using( StringWriter sw = new StringWriter() )
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, partialPath);
ViewContext viewContext = new ViewContext(controller.ControllerContext,
viewResult.View,
( viewData == null ) ? controller.ViewData : viewData,
controller.TempData,
sw);
// copy retVal state items to the html helper
foreach( var item in viewContext.Controller.ViewData.ModelState )
{
if( !viewContext.ViewData.ModelState.Keys.Contains(item.Key) )
viewContext.ViewData.ModelState.Add(item);
}
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
Conceptually, the procedure to follow involves using the ViewEngines defined for your app to find a partial view by its name. You then create a ViewContext off of that partial, and copy the various model state properties over to it.
The code assigns an optional ViewDataDictionary that you can provide to the ViewContext. If you don't provide the ViewDataDictionary it grabs the ViewDataDictionary defined for the controller that it's being called against.
What this means is that you can either define ViewData values (or ViewBag properties) directly in your controller and then call the extension method -- which will apply those ViewData/ViewBag properties to the partial when it gets rendered -- or you can create a separate ViewDataDictionary object in your action method and pass it to the extension method. The first is quicker/easier, but it "pollutes" the ViewData for your action method, while the second takes a little longer to set up but lets you keep your partial view data separate from your action method's ViewData.
Look into the MvcMailer project. Coupled with a partial view that renders your table, you should be able to pretty easily put together emails with your tables.
Render a view as a string
I use something simple like above but I almost always create separate views for the emails. Mainly due to the need to use absolute links and inserting the CSS into the head.

ASP.NET MVC pass model to an html template without controllerContext

I'd like to have an html template I'm using for emails. I want to pass a model to it, have it rendered and returned to a string variable.
How can I accomplish this? I'm sure I can use the rendering that comes with asp.net mvc.
I do not want to do this inside my controller or using the controllercontext object.
Using a comment from #Mitch Rosenburg as the answer:
I'm using DotLiquid templates.
I haven't tried it myself, but I found a library called Postal that I'm planning to use for a very similar feature in my own apps.
You can do it like in the following method:
private string ViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
And in your action method you can use it like this
ViewToString("Index", model);
where model is the model that you want to send to your 'template'
I had to do the same thing once, and I ended up using HttpWebRequest to "render" the view and capture the result as a string.
This solution came with a whole host of other issues (getting the authorization cookie, powering through our custom url security layer, etc), and it felt inefficient and sloppy, but deadlines are deadlines, right?
Code looked something like this:
public string GetEmailBody(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
return streamReader.ReadToEnd();
}
Hope it helps somebody.

Resources