C# Handle GUI in MVC - c#

I am using the asp.net MVC Framework. IN my application a user has to log in. And when the combination of username and password is correct, the div (or panel?) with with the menu in it, must become visible. But how can I do this? When a name my panel pnlMenu, in my controller i cannot do something like:
pnlMenu.visible = true;
So, how do i have to do this?

What you should do is in your controller check to see if a user is logged in and set a value in the ViewData like this:
ViewData["IsLoggedIn"] = true;
Then in your view you can set the visibility of the method based on this value. This way if you change the view later, or decide to have multiple views, they can each use this value and there isn't any coupling between your view and your controller.

Create a method or property on your View that enables you to hide or show the appropriate controls ?
Then, in your Controller you can access that property or method of your View, can't you ?
You do not want to reference specific 'controls' on your View in your controller, since, one of the ideas of MVC is that you can just replace the UI with another implementation (web / win / ...) and make use of the same controllers and application logic.
Then, you just want to describe an operation that your View should support, so, in the interface that describes the 'contract' that your View must support, you should create a method which is called 'ChangeState( bool loggedIn )' for instance.
In the controller, you can call this method when the user has logged in.

Related

ASP.NET Core 6 How to redirect from one view to other

tl:dr:
I would love to redirect from my Layout view into an index view of a certain class. I do not want to do it through a button.
After I log in the layout page changes depending on your role. If you have a regular user role I want you to be immediately redirected to another view.
This the part of the code where I would like the redirecting to take place. In the "else" clause.
Is there a simple way about please?
I was thinkig of calling a method from a controller of said class which would simply have the redirect method inside. But that has failed since I was not able to call such method simply in a view and I failed to find a way after a long google search.
Thank you for your help.
According to your description, if you want to redirect the view from layout to another view, I suggest you could consider using the Context.Response.Redirect("~/Account/LogIn");
More details ,you could refer to below example:
#if (Context.User.Identity.IsAuthenticated)
{
}else
{
Context.Response.Redirect("/home/Privacy");
}

In MVC architecture, can a controller and model manage more than one View?

Can I create 2 views for a model and controller?
My current application is MVC complaint , and it has a single view.
I need to create a second GUI, that fetches few information from the model (updated from the first GUI data) , update it and display back in the first GUI.
You can have a different view per action. Based on your description, this seems to be what you are looking for. You have different actions for the same model, it's ok.
You can add a new action for the new information that you want to update and click on the right button of the mouse and click "add view". It will add a new view for that action.
The most common way to do this is to create other actions, one for each View you want to display.
You can have as many Views as you like, as long as you can route between them.
As default, the line return View(); or return View(model) will look for a view in this path: /Views/{ControllerName}/{ActionName}.
You can also specify the view name, as Controller.View() also accepts a string as the view name.
Knowing this, you could display different views from a single action, according to parameters passed to your action.
Example:
public ActionResult Example(bool a)
{
if (a) return View("a");
else return View("b");
}
this will call the view /Views/{ControllerName}/a.cshtml if a is true and /Views/{ControllerName}/b.cshtml if a is false.
You can also call other partial views or actions from your first view, using html helpers: #Html.Partial({ViewName}), #{Html.RenderPartial({ViewName});} or #{Html.RenderAction({ActionName});}
EDIT:
View() also searchs for /Views/Shared/{ActionName}

Custom controller for the layout.cshtml

I have an MVC website which I wanted to implement a globalization/localization. On my _Layout.cshtml, I have a dropdown which have the languages I supported. When a user selected a language on that dropdown, it should automatically post to the server then render the new language.
Is it possible that I create a specific custom controller for the _Layout.cshtml only? If yes, how? If no, is there any possible way or approach I can make?
Thanks in advance!
You should move that to a child action, then call the child action from the layout view.
You can make the form POST to a separate action (in a controller shared with the child action) that sets the cookie / session / DB property, then redirects back to the original URL (via Request.UrlReferrer or from a hidden model-bound field).

How to implement logic at "masterpage" level

I'm still new to MVC, so bear with me :-)
I've got a community site I'm working on, and I'd like to show how many users are online on all my pages after the user's been logged in.
I've got a shared view which is used as layout for all pages after login (UserLayout.cshtml)
Can I somehow add the logic to show online count to my shared layout ?
If it were WebForms I'd just have some code-behind for my masterpage, but this is obviously not an option here.
The information about users online is fetched from a cache. It's not available as a property on any of my View Models.
You can write an action which renders the information (using a very small view)
You can then call Html.Action to render it from the layout page.
You can create a 'UserLayoutModel' class and have all other view models derive from it. You can also use 'RenderAction' to have a part of the UI rendered separately (make sure you mark this action with ChildActionOnly attribute).
What I did was create a BaseController.cs that all controllers inherit from, and in the base controller you can override OnActionExecuting and any viewdata values you set here will be available to your master page.
protected override void OnActionExecuting(ActionExecutingContext filterContext) {
base.OnActionExecuting(filterContext);
}
You can create a Global Action Filter.
Normally you add an Action Filter as an attribute to a method or class ([HttpPost]). Using a global Action Filter you can add code to every Action, without the need to inherit from a specific class. It is like you added an attribute to each and every Action method.
This article explains a lot.

What's the best place to put default content in an MVC application?

I'm working on a sort of a CMS/Wiki application to help me experiment with the new Asp.Net MVC framework, and I'm trying to wrap my head around some of the code organization.
Right now, I have three views that cover displaying an article: Index, Edit, and Rename. All three views display the contents of the current page, or placeholder content stating that the page does not exist.
This is currently accomplished with the following code in the action method for each view:
MyPage myPage = null;
if (!string.IsNullOrEmpty(pageName)) {
myPage = mRepository.GetMyPage(pageName);
}
//Page does not exist.
if (myPage != null) {
ViewData["pageContent"] = myPage.GetParsedSource(new PageState());
ViewData["pageSource"] = myPage.Source;
ViewData["title"] = myPage.Title;
}
else {
ViewData["title"] = pageName;
ViewData["pageContent"] = "Page does not exist, feel free to create it!";
ViewData["pageSource"] = "";
}
ViewData["pageName"] = pageName;
My question is, where should this logic actually go?
1) The Controller (as it is now), which requires the above code to be replicated across action methods?
2) The Model, defaulting values for pageSource to the verbiage shown above? This would have the downside of moving display text into the model.
3) The View, using a null coalescing operator to convert null ViewData entries to their defaults?
4) In the Views, but add additional controllers to handle cases where the pageName does not exist.
EDIT:
Hopefully this should clarify things a little. The flow of the application is as follows:
When the user enters a URL (i.e. /pages/page_title), they arrive at a screen which displays the content of the article, along with hyperlinks labeled "edit" and "rename."
Clicking edit displays a page which contains the article content, as well as form controls to edit the article's source.
Clicking rename displays a page which contains the article content, as well as form controls to edit the article's name.
I would have several actions:
Lookup
Display
Create
Edit
Rename
In your default Lookup controller action (which gets hit when the user asks for, say, "/wiki/article-title"), you can redirect (RedirectToAction()) to the appropriate action as necessary. That encapsulates your Create logic into its own controller, and can also be called directly (RESTful). Same with the others. That also allows you to keep your views very, very stupid (always a good thing).
I would keep it in the controller but extract it out so that you don't have to replicate the code in each of the actions.
Maybe set some defaults in the controller's constructor and then have a separate private method (ie. not an action method) that takes your MyPage object and sets the viewdata that is shared between your actions.

Categories

Resources