Broken RenderPartial After Upgrade To ASP.NET MVC2 - asp.net

I upgraded a MVC1 project to MVC2, now all my calls to RenderPartial are throwing
System.ArgumentNullException: Value cannot be null.
However this does works:
<% Html.RenderPartial("~/Views/Shared/LogOnUserControl.ascx"); %>
And this does not (works in MVC1):
<% Html.RenderPartial("LogOnUserControl"); %>
Did the behavior of RenderPartial change?

Bleh.... found the problem, my project was referencing MVCContrib 1.0, downloaded the latest build and referenced that instead fixed the issue.

Your call to
<% Html.RenderPartial("LogOnUserControl"); %>
seems to be working fine otherwise you would be receiving
The partial view 'LogOnUserControl' was not found. The following locations were searched....
Considering that
When Html.RenderPartial() is called with just the name of the partial view, ASP.NET MVC will pass to the partial view the same Model and ViewData dictionary objects used by the calling view template.
and
ArgumentNullException is thrown when a method is invoked and at least one of the passed arguments is a null.
It seems like the Authentication arguments are not being passed properly to LogOnUserControl or maybe you have customised it in someway?

Related

T4MVC - Does not generate parameter-less constructors?

I'm trying to use T4MVC in my ASP.NET MVC4 application.
I'm inserting it in my view as shown below:
#using (Ajax.BeginForm(MVC.Check.Lead(),
The Lead method takes an integer ID parameter which is supplied in the form itself.
In the documentation T4MVC state that a parameter-less constructor should be auto generated however the compiler says that there isn't.
Why is it not generated?
Well I figured it out myself. My controller action returned a string which apparently is not suppported by T4MVC. So I changed it to partialviewresult and it works now.
Another possible reason (for future viewers) is that your action is inherited. It works but T4MVC will not automatically make your base actions virtual and it will not generate the parameter-less overload.

asp.net mvc3 to mvc4 conversion exception in only some pages

I am changing an MVC3 app to MVC4(all the proper changes have been done in web.config). The application is loading fine as are some pages but some pages which were working previously in MVC3 are throwing the same kind of error as below: I have done the references correctly and no code was changed during the conversion process
Can anyone tell me the solution for this error
Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'
This happens when I call this method in a cshtml like this
#{ Html.RenderAction("GetTestItem", "TestItemsInCart", Model.TestItemsList);}
and the GetTestItemmethod is
[HttpGet]
public ActionResult GetTestItem(TestItemsList test)
{
TestViewModel xyzViewModel = new TestViewModel ();
return PartialView("_TestXyz", xyzViewModel );
}
The method is getting called in the controller and it is returning the result to the view only in the cshtml I am getting the stated exception.
This was working previously in mvc3
Thanks
Here are the steps that you need to follow to manually upgrade an existing ASP.NET MVC 3 application to MVC 4: http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253806
Also the error message that you are getting indicates that you have some missing closing braces in your Razor view. Make sure you have fixed it or show your Razor view if you expect other people be able to help you.
Check for opened brackets. Any opened bracket { needs to be closed with another bracket }.
Check for naming of the variables.

ASP.Net MVC3 Razor Href Error

For some reason this line of code.
#Html.DisplayFor(modelItem => item.api_name)
is throwing the error.
Compiler Error Message: CS1963: An expression tree may not contain a dynamic operation
Web search has been no help I can't seem to figure out whats wrong with the code.
The page is not strongly typed.
As the error states, you can't use dynamic models with type-safe editor helpers.
You should use typed models instead. (using #model SomeType)
Is your view strongly typed? If not, you need to have a strongly typed view, as lambdas don't support dynamic members.
Just add this to the top of your view:
#model YourModel

Downgrading from ASP.net 4 MVC 2 to ASP.net 3.5 MVC

I am a bit of beginner with ASP.net MVC, and built most of my original app while following a book.
I am trying to downgrade a project because of server limitations.
I have resolved a number of errors, but now I am stuck on this on:
If I build it builds ok, but when I press play, I get this error:
Compiler Error Message: CS0246: The type or namespace name 'dynamic' could not be found (are you missing a using directive or an assembly reference?)
It occurs on this line:
[System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()]
Line 142: public class views_rooms_index_aspx : System.Web.Mvc.ViewPage, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler {
Line 143:
Line 144: private static bool #__initialized;
Which I believe probably originates from somewhere like this in my View:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
Is System.Web.Mvc.ViewPage something that you can only do in asp.net 4? What do I need to replace this with for ASP.net 3.5?
The dynamic keyword was introduced in .NET 4.0, and will not be available in any version earlier than that.
I believe the default base class for an mvc view in MVC with ASP.NET 3.5 was System.Web.Mvc.ViewPage, without any generic type. If you want to add a strongly typed model (which is recommended whenever you want to pass data from the controller to the view) you should create a view model class, and replace dynamic with the namespace qualified name of your view model.
Example: you want to pass a string name that you got from somewhere in your controller, to the Home/Index view. Do the following:
Create a class HomeIndexViewModel (name doesn't matter, but this is a good one ;) ) in the Models folder of your project, and give it a public string Name {get; set;} property.
In your controller, insantiate this class and set the name. Pass it to the view using the return View(model); overload.
Make your view inherit System.Web.Mvc.ViewPage<YourProject.Models.HomeIndexViewModel>. You can now access the name using Model.Name in the view.
Your Inherits attribute:
Inherits="System.Web.Mvc.ViewPage<dynamic>"
Has the dynamic type as the generic type - change it to the correct ViewModel type.
For example:
Inherits="System.Web.Mvc.ViewPage<ProductViewModel>"
Answers above cover it pretty well.
you might consider using:
http://www.jetbrains.com/resharper/
i have used it to convert my website to web application and it really helps.

ASP.NET MVC 3 Rendering a view "polymorphically" using DisplayFor(TBase o)-like syntax

Using ASP.NET MVC 3 I would like to render different "templates" (i.e. partial views or whatever you want to call them) to my views based upon the type of the view model (or a property on the view model) supplied at runtime.
I understand the out-of-the-box ASP.NET MVC functionality enables the addition of a UIHint attribute to a property or field on the viewmodel supplied to the view, but as far as I can tell this doesn't meet my needs as I won't know the template required until runtime.
AFAICT ASP.NET doesn't support generic viewmodels of type T either, so my idea is to take the ASP.NET MVC3 source code and write my own DisplayFor method that accepts a viewmodel instance (specified by it's abstract base type in the view), which resolves the actual type at runtime and uses this to find the template name by convention.
Does this sound feasible/worthwhile?
The view would look thus (note specification of base type, a concrete instance would be supplied at runtime):
#model MyViewModelBase
#{ DisplayFor(Model); }
Viewmodel:
public class MyMagicalViewModel : MyViewModelBase {}
Invocation of this view would look thus:
...
return View("MyView", MyMagicalViewModel);
...
...and this would return html corresponding to the MyMagicalViewModelPartialView.cshtml
Have you looked into Html.EditorFor?

Resources