localization for mvc ajax form and partial view - ajax

I have used resources file to create multi langual mvc3 application. In _viewStart.cshtml I have these two lines which makes it that loclization works for all views except partial views which is rendered from a ajax.form
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(AsoMvcApp.MySession.Current);
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(AsoMvcApp.MySession.Current);
when I check the CurrentUICulture for a partial view which is rendered from an action which is called from a ajax form it is still default and not chnaged. when I add those to lines of code in the begining of the partial views it works fine. but I don't want to add it to all partial views. it must be a better way.

it must be a better way.
Yes, instead of putting this code in your _ViewStart.cshtml put it in the Application_AcquireRequestState method in your Global.asax

Related

Call a regular view in Html.Partial?

I have a regular view I'd like to use in another page, appearing magically in a jQuery-like accordion if the expand button is clicked. If I call it using:
Html.Partial(A_non_partial_view, new view_model_used_by_the_non_partial_view())
...does that have a chance of working, or is MVC not plumbed that way? (I'm using MVC 3 if that helps.)
You can, but only if it is in the same controllers views folder or in the shared views folder. otherwise you will have to specify that path fully which isn't practically really.
If you use Html.Action or Html.RenderAction, then that action will need to return a PartialView otherwise it will push out a full html page again with head tags etc etc

I cannot add a partial view in vs2010

When I try to add a partial view using asp.net mvc 3 and razor it just add's a normal .cshtml file and not a partial view. has anyone had this problem before?
/Martin
In Razor there is no notion of partial views as there was in WebForms. You only have templates with the .cshtml extension. Depending on how the controller action serves this view (with either return View() or return PartialView()) the _Layout.cshtml will be applied or not. Also if you render a template with either the Html.Partial helper this template will be considered as a partial view. Finally you have the possibility to set whether you want or not a Layout from inside the template itself. For example to disable it:
#{
Layout = null;
}
So basically in Razor you have templates and you could consider partial views as templates without a layout.

MVC3 Finding a control by its Name

I have a C#.Net web app and I am trying to access one of the HTML/ASP Text Boxes in the Controller for the Edit View of my Proposal model. In a non-MVC app, I was able to do this using Control.ControlCollection.Find(). Is there an equivalent for a MVC3 project?
You ask for an equivalent of Control.ControlCollection.Find() in MVC?
In MVC your controller is not aware of controls.
The controller just receives data via parameters and returns data via the function result.
What do you want to do with the control in your controller code?
If you want to access the value, you should bind it to a parameter:
View:
<input name="MyControl" type="text" />
Controller:
public ActionResult MyAction(string MyControl) {
// MyControl contains the value of the input with name MyControl
}
The MVC pattern was designed to keep things separated.
The View has no knowledge of the controller at all
The Controller only knows that a view exists and what kind of data that it needs. It do not know how the data is render.
Hence, you can never get information about controls/tags in the view from the controller. You need to use javascript/jQuery in the view and invoke the proper action in the controller.
In an MVC-application you don't have controls like in a webform-application.
In MVC you collect your required data in the controller and pass it to the view.
Typicaly the view is a HTML-page with embedded code.
In opposite to controls in webforms which produce HTML and handles the post-backs in MVC you have to do all this manually. So you don't have controls with properties and events wich you can access easily in the controller and you have to handle all your posts with your own code.
Thats sounds as it is a lot of more work - and indeed it could be if you implement the behaviour of complex controls - but MVC applications are much better to maintain and you have 100% influence to the produced HTML.
Well probably i am late for this but it should help others in future...u can store ur value in hidden field in view and then access that value in controller by following code..
Request.Form["hfAnswerOrder"].ToString();
Point - hfAnswerOrder is the ID of the hidden field
My Control in cshtml page..
#Html.Hidden("hfAnswerOrder", Model.Answers.ToList()[0].AnswerOrder)

How to load a Razor view and render it on the fly?

I have a custom CMS built with ASP.NET WebForms (you can see it in action at Thought Results). Now I want to build it using ASP.NET MVC 3 (or even 4). I don't want to change the architecture that much, therefore, I need to dynamically load a Razor View, and dynamically run a Model Loader method, and give the model to the view dynamically, then render the view, and return the result rendered string, all done in server.
In ASP.NET WebForms, my code is:
string renderedString = "LatestArticles.ascx".LoadControl().GetReneredString();
Now, I'd like to be able to write a code line like:
string renderedString =
"LatestArticles.cshtml".LoadView().BindModel("ModelBinderMethodName").Render();
I know about many questions about rendering a view (view to string), but I didn't find what I want.
You may checkout RazorEngine.

How does ASP.NET MVC arbitrate between two identically named views (aspx and razor)?

Using ASP.NET MVC3 I created a new Razor view and gave it the same name as the existing .aspx view that I had been using. I noticed that controller continued to pick up the .aspx view (which has the same name as the action) which is pretty much what I expected. I then renamed the .aspx view and action picked up the razor .cshtml view.
So if I have two views called myview.aspx and myview.cshtml and an Action called MyView() that does a return View(), it will pick up the myview.aspx view and return that.
How does MVC3 decided which view-type to default to?
Is there a way to change this default behavior to prefer a razor view over an .aspx view?
Everything stems down to the order of view engines in the ViewEngines.Engines collection. Here's how the ViewEngines static constructor looks like (as seen with Reflector in ASP.NET MVC 3 RTM):
static ViewEngines()
{
ViewEngineCollection engines = new ViewEngineCollection();
engines.Add(new WebFormViewEngine());
engines.Add(new RazorViewEngine());
_engines = engines;
}
which explains why WebForms is the preferred view engine.
So you could perform the following grotesque hack in Application_Start to inverse the preference towards Razor :-)
var aspxVe = ViewEngines.Engines[0];
var razorVe = ViewEngines.Engines[1];
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(razorVe);
ViewEngines.Engines.Add(aspxVe);
I would imagine its down to the order in which view engines are registered. Earlier registered view engines will be queried first. If you want to change the order:
ViewEngines.Engines.Insert(0, ...);

Resources