I have a viewpage and a partialview on that. For default request-when page is called- CultureInfo is set in base class of viewPage. I have created this custom base class which initialize the culture depending on a cookie and does some other stuff as well. There is a search button on the view page which updates a partial view on the page with Ajax call (jquery Ajax call). There is separate action for this partial view update call which returns partial view. The problem is partial view do not have any base class and when resources are displayed on partial view they are displayed for default culture and not the current culture. What would be best approach to set cultureInfo when partial view is called from jQuery Ajax call?
I have this mvc application as part (area) of bigger existing application so I can not touch session_start.
I'm not sure why you need to pass the culture around, can't you get it directly from your PartialView using a helper method?
#YourApp.CultureHelper.GetCurrentCulture()
which is just calling
public static string GetCurrentCulture()
{
return Thread.CurrentThread.CurrentCulture.Name;
}
If you are doing any work with internationalization and ASP.NET MVC I highly recommend reading through these two excellent posts by Nadeem Afana
ASP.NET MVC 3 Internationalization
ASP.NET MVC 3 Internationalization - Part 2 (NerdDinner)
Very well written and explains everything you need to know about internationalization.
You can send the culture info to your partial view as model with string array.
I guess you have handled the ajax call so I am just gonna show you the way of passing the value. The below one is your controller :
public ActionResult Index() {
return PartialViewResult("samplePartial", new string[] { "en" });
}
And this is your partial view :
#model string[]
#if(Model[0] == "en") {
#:Culture is "en"
} else {
#Culture is different than "en"
}
Also this can be also rendered inside your view as indicated below :
#Html.Partial("samplePartial", new string[] { "en" })
Related
I have this contact form which I want to use on two pages (two views) in MVC 5.x (razor viewengine) So I have put the form in an partialview called _Contact and I have read that RenderAction is the best approach if you do not have the required data for the partialview in your model and if it is more standalone (seperate from the rest of the view)
So I call it like this:
#{ Html.RenderAction("SendMail", "Uk");}
My Uk controller has these two methods:
[HttpGet]
public PartialViewResult SendMail()
{
return PartialView("_Contact");
}
[HttpPost]
public PartialViewResult SendMail(FormCollection fc)
{
// send mail using values out of the form (sorry did not feel like building a complete model for it
ViewBag.Succeed = true;
// if smtpclient could not reach server etc. it returns false
return PartialView("_Contact");
}
it all works, but the PartialView is only rendered, not on the placeholder where i call the RenderAction. It works all great, but after the post it just displays the partial view and not the "parent view" and the shared layout view etc. I hope that I made myself clear. Please let me know if I need to add more info.
This is the BeginForm from my shared view:
using (Html.BeginForm("SendMail", "Uk", FormMethod.Post))
It will not work as expected for your current code, because when you post the form, it returns Partial View not complete View. If you want to get only partial view then you have to submit your form via Ajax.
In ajax's success handler you will get HTML of your partial view and that you can put in a DIV tag of partial view container.
This link will give you a better idea about Posting Partial View via Ajax.
ASP.NET MVC Partial view ajax post?
Please explain me how to create update panel in ASP.NET MVC4 application. I looked for many blogs... but can not find any useful way.
This is my view
How I can separate these actions in same view?
Your two panels don't allow the user to switch between one or the other so I assume that you have an "intro" view with the option to either Sign in or Register. Right? In that case there is no real need for client side panel switching using Javascript/Ajax. Your "intro" view can pass a parameter to the controller action defining whether it needs a Sign In or Register view back.
For instance:
// RouteConfig.cs
routes.MapRoute(
name: null,
url: "login-register/{action}",
defaults: new { controller = "Authentication"},
constraints: new { action = #"(SignIn|Register)" }
);
// AuthenticationController.cs
public ActionResult SignIn()
{
...
return View(); // Will return the Authentication\SignIn.cshtml view
}
public ActionResult Register()
{
...
return View(); // Will return the Authentication\Register.cshtml view
}
Update panel does not really exist in ASP.NET MVC. It used to be there in ASP.NET Web form develeopment world before people actually realized it is better to use hand written jQuery ajax for doing the partial page update.
You may use jQuery ajax methods to post your form data to an action method and do partial page update to the page as needed. You may also consider using Partial view (to return a part of a page) as required.
In your case you can create 2 partial views for Sign in and Register and include those in your main view, to make your code more reusable.
<h1>Login or Register</h1>
<div>
#Html.Partial("Login")
</div>
<div>
#Html.Partial("Register")
</div>
How can I pass parameters to a partial view in MVC3 (razor). I replaced a regular View page with a Partial View in my MVC project. For a regular View page, I passed parameters like
public ActionResult MeanQ(int id)
{
Access access= db.Access.Find(id);
return View(access);
}
Now since I changed the view to a partial view, I have the following code instead:
public ActionResult MeanQ(int id)
{
Access access= db.Access.Find(id);
return PartialView("_MeanQPartial");
}
but do not know how I can still pass the parameter 'id' to make it work like before. Please help. For what its worth, the View or the partial View , both are triggered by a link and displayed in a Jquery Modal Dialog box.
Try this
return PartialView("PartialViewName", access);
Simply give it as 2nd parameter. PartialView method has 4 overloads and this includes one with two parameters PartialView(string viewName, object model)
public ActionResult MeanQ(int id)
{
Access access= db.Access.Find(id);
return PartialView("_MeanQPartial", access);
}
For what its worth, the View or the partial View , both are triggered by a link and displayed in a Jquery Modal Dialog box.
View would return an entire page using your layout. PartialView only returns the HTML from your partial. For a modal dialog, the partial is enough. No need to retrieve a complete page.
How can I get the actual "Main-Controller" in a RenderAction?
Example:
MyRoute:
{controller}/{action}
My url my be:
pages/someaction
tours/someaction
...
In my Site.Master I make a RenderAction:
<% Html.RenderAction("Index", "BreadCrumb"); %>
My BreadCrumbController Action looks like this:
public ActionResult Index(string controller)
{
}
The strings controller contains "BreadCrumb" (which is comprehensible because actually I am in BreadCrumbController).
What's the best way to get the "real" controller (e.g. pages or tours).
Parent view/controller context
If you use MVC 2 RC (don't know about previous releases) you can get to parent controller via view's context, where you will find a property called:
ViewContext ParentActionViewContext;
which is parent view's context and also has a reference to its controller that initiated view rendering...
Routing
It seems to me (from your question) that you have requests with an arbitrary number of route segments... In this case you have two options:
Define your route with a greedy parameter where actions in this case will catch all actions in your request URL
{controller}/{*actions}
Create a custom Route class that will handle your custom route requirements and populate RouteData as needed.
the second one requires a bit more work and routing knowledge but it will help you gain some more knowledge about Asp.net MVC routing. I've done it in the past and it was a valuable lesson. And also an elegant way of handling my custom route requirements.
Could you pass it as a parameter to the controller?
--Site.master--
<% Html.RenderAction("Index", "BreadCrumb"
new { controller = ViewData["controller"] }); %>
--BreadCrumbController.cs--
public ActionResult Index(string controller)
{
}
--ToursController.cs--
public ActionResult SomeAction(...)
{
// ....
ViewData["controller"] = "Tours"
// You could parse the Controller type name from:
// this.ControllerContext.Controller.GetType().Name
// ....
}
What do you mean with "real" controller? Your action points to one controller.
Do you mean the previous controller? So: the controller that was used to render your view where your link was created that points to your breadcrumbcontroller?
Unless you add the name of that controller to the link as a parameter, there is no way to get to that.
Are there any good examples of mvc routing wherein every 404 page not found request is routed to a standard view in MVC which basically pulls the content from the database.
Just add this route to the bottom of your RouteTable:
routes.MapRoute("DynamicPages", "{*page}", new { Controller = "DynamicPages", Action = "Show", Page = String.Empty });
And create a controller for displaying dynamic pages from db:
public class DynamicPagesController : Controller
{
public ActionResult Show(string page)
{
var pageContent = DB.GetContentForPage(page);
return Content(pageContent);
}
}
Here's one way to do this: In your global.asax file in Application_Start, you need to set the default controller factory. Override it with an instance of your own factory.
void Application_Start()
{
ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
}
MyControllerFactory should inherit from DefaultControllerFactory and when selecting the controller to use, look in your database for the appropriate page you want to display. If the page exists, select the appropriate controller and override the action in the requestContext.RouteData collection to point at the appropriate action for displaying dynamic pages.
If the requested page doesn't exist, pass back a call to the base method and let it do what it would normally do.
There are other ways you could do it, but this one should work and allows you to intercept the request before you hit the 404 page.
modify the web.config file, you may Reference to this page and look at the setting custom error pages in web.config section.