how to pass model to partialview in umbraco? - asp.net-mvc-3

I want to pass a object as model to a partial view in umbraco. there is two function #Html.Partial() and #Html.Partial() which have 4 override method listed below :
#Html.Partial(string partialName)
#Html.Partial(string partialName, Object object)
#Html.Partial(string partialName, Object object, ViewDataDictionary dic)
#Html.Partial(string partialName, ViewDataDictionary dic)
and :
#Html.RenderPartial(string partialName)
#Html.RenderPartial(string partialName, Object object)
#Html.RenderPartial(string partialName, Object object, ViewDataDictionary dic)
#Html.RenderPartial(string partialName, ViewDataDictionary dic)
I try to use #Html.RenderPartial(string partialName, Object object) and
#Html.Partial(string partialName, Object object) but i an getting this Exception:
Cannot bind source type <>f__AnonymousType0`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to model type Umbraco.Web.Models.RenderModel.
I search a lot in google to an example of using these methods but i cannot find any thing. so there are two question:
What is different between Partial and RenderPartial method?
How can I pass data to partialview from page razor?

I found solution. my PartialView was inherited from :
Umbraco.Web.Mvc.UmbracoTemplatePage
now i change its inheritance to :
#inherits Umbraco.Web.Mvc.UmbracoViewPage<MyModel>
and then I can add mymodel object to RenderPartial() Method.

Related

MVC ViewModel Index problems

I see this error on my page
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List1[sucu1.Entities.Inventory]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable1[sucu1.Models.InventoryViewModel]'.

How to set model to IEnumerable in MVC3.0

I want to use a view with model as IEnumerable type some thing like this
#model IEnumerable<WCG.Data.EntityObjects.FaxHistory>
Note:-I have classes as FaxHistory.cs(Main class where variables are declared)
FaxHistoryBo.cs
FaxHistoryDao.cs
IfaxHIstoryDao.cs
FaxHistoryController.cs
FaxHistorymodel.cs

MVC invoke model binder directly on a single object

Is there a way that I can invoke the model binder for a single object?
I don't want/need a custom model binder - I just want to do something like this:
MyViewModel1 vModel1 = new MyViewModel1();
InvokeModelBinder(vModel1);
MyViewModel2 vModel2= new MyViewModel2();
InvokeModelBinder(vModel2);
And when I'm done, the properties of both vModel1 and vModel2 have been bound to what's in the incoming request. Because of the way that our controller/action is being written, I don't necessarily want to list vModel1 and vModel2 in the action method's input list (since there will end up being a potentially long list of view models to optionally bind against).
Use Controller.UpdateModel:
MyViewModel1 vModel1 = new MyViewModel1();
UpdateModel(vModel1);
Update
Note if ModelState in controller has validation errors (related to model passed in action), UpdateModel (with any model) throws excetion, despite UpdateModel success and vModel1 is updated. Therefore errors in ModelState should be removed, or put UpdateModel in try/catch and just ignore excetion
This is wrong on many levels IMHO:
This is not how ASP.NET MVC is designed to work.
Your actions do not define a clear contract of what data they expect.
What do you get out of it? Smells like bad design.
Model binding is driven by reflection. Before an action is invoked it will reflect the method parameters list and for each object and its properties it will invoke a model binder to find a value for each property from the various value providers (form POST values provider, url parameters, etc). During model binding the ModelState validation is done as well.
So by not using the default ASP.NET MVC to do this you are losing all that.
Even if you were to manually get hold of a model binder like that:
IModelBinder modelBinder = ModelBinders.Binders.GetBinder(typeof(MyObject));
MyObject myObject = (MyObject ) modelBinder.BindModel(this.ControllerContext, ** ModelBindingContext HERE**);
You can see that you need to initalize a ModelBindingContext, something that ASP.NET MVC will do internally based on the current property it is reflecting. Here is the snipped from the ASP.NET MVC source code:
protected virtual object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) {
// collect all of the necessary binding properties
Type parameterType = parameterDescriptor.ParameterType;
IModelBinder binder = GetModelBinder(parameterDescriptor);
IDictionary<string, ValueProviderResult> valueProvider = controllerContext.Controller.ValueProvider;
string parameterName = parameterDescriptor.BindingInfo.Prefix ?? parameterDescriptor.ParameterName;
Predicate<string> propertyFilter = GetPropertyFilter(parameterDescriptor);
// finally, call into the binder
ModelBindingContext bindingContext = new ModelBindingContext() {
FallbackToEmptyPrefix = (parameterDescriptor.BindingInfo.Prefix == null), // only fall back if prefix not specified
ModelName = parameterName,
ModelState = controllerContext.Controller.ViewData.ModelState,
ModelType = parameterType,
PropertyFilter = propertyFilter,
ValueProvider = valueProvider
};
object result = binder.BindModel(controllerContext, bindingContext);
return result;
}

Is it possible to render a view outside a controller?

I wanted to know if it was possible to render a view from a class that is not a controller. Everything I see seems to say that you can't.
What I'm trying to do is to render a partial view from a WCF web service in order to push it somewhere else. Is it possible to use the view engine for that?
Thanks!
Update:
I keep getting argument null exception with the HtmlHelper. Here is my code and the stack trace. My partial is indeed named TableOfContent.cshtml and is located in the /View/Shared folder. Do I new to instantiate my ViewContext differently?
HtmlHelper helper = new HtmlHelper(new ViewContext(), viewData);
var a = helper.Partial("TableOfContent");
at System.Web.Mvc.ViewContext..ctor(ControllerContext controllerContext, IView view, ViewDataDictionary viewData, TempDataDictionary tempData, TextWriter writer)
at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName)
at SyncInvokeProcessEvent(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
This will call the view without requiring a controller (for the partial view).
Html.Partial(partialViewName);
See also Html.Partial method overload
Here are two different ideas to consider:
I've done something similar using RazorEngine. Allows you to render razor templates to a string.
Create a controller and use WebClient to invoke the action. This assumes you have an MVC application.

Need to allow an action method string parameter to have markup bound to it

I have an action method that takes in a string as its only parameter. The action method transforms it, and returns the result back to the client (this is done on an ajax call). I need to allow markup in the string value. In the past, I've done this by decorating the property on my model with [AllowHtml], but that attribute cannot be used on a parameter and the AllowHtmlAttribute class is sealed, so I cannot inherit from it. I currently have a work around where I've created a model with just one property and decorated it with the aforementioned attribute, and this is working.
I don't think I should have to jump through that hoop. Is there something I'm missing, or should I make a request to the MVC team to allow this attribute to be used on method parameters?
If you need to allow html input for a particular parameter (opposed to "model property") there's no built-in way to do that because [AllowHtml] only works with models. But you can easily achieve this using a custom model binder:
public ActionResult AddBlogPost(int id, [ModelBinder(typeof(AllowHtmlBinder))] string html)
{
//...
}
The AllowHtmlBinder code:
public class AllowHtmlBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var request = controllerContext.HttpContext.Request;
var name = bindingContext.ModelName;
return request.Unvalidated[name]; //magic happens here
}
}
Find the complete source code and the explanation in my blog post: https://www.jitbit.com/alexblog/273-aspnet-mvc-allowing-html-for-particular-action-parameters/
Have you looked at ValidateInputAttribute? More info here: http://blogs.msdn.com/b/marcinon/archive/2010/11/09/mvc3-granular-request-validation-update.aspx.

Resources