MVC how to include a view that does not have layout info - asp.net-mvc-3

I'm trying to reference a view, but do not want all the header and footer that comes with the view because the viewstart references layout page. When I have a popup, it also shows the menu items in the header. Is there a way not to include the layout.cshtml?

Layouts can be specified directly in the view or based on the caller.
For example if your ActionMethod returns like this:
return View();
The view will be rendered with the layout. However if the ActionMethod returns like this:
return PartialView();
Then the rendered view will not have a layout.
However, this can be overridden in the view itself. In your view, if ViewBag.Layout is null the layout will not be included. Inversely, if ViewBag.Layout has a value that layout will be used, reguardless of how the view is called. For this reason, most views do not set ViewBag.Layout directly, and leave it up the the caller to specify the intent.
Hope his helps.

All you have to do is explicitly set layout to null.
#{ Layout = null; }

Put this in the header of the view
#{ Layout = null; }
Or something like that

Related

how to update ObservableCollection<> of one page from another page in xamarin

My application is in MVVM architecture.
I Have photo.xaml page in which i have one 1 ListView whoose bindingcontext is ObservableCollection listphoto of photos which is defined in its viewmodel.cs file.
now i have to redirect to BarcodeScan.cs from button click of photo.xaml .
my que i how can i add item to listphoto from here(BarcodeScan.cs )??
I tried to define new list in BarcodeScan like this
public ObservableCollection<JobPhoto> ListSerialNumbers { get; set; }
and intialised in its constructor like this
ListSerialNumbers = new ObservableCollection<JobPhoto>();
but it dont update list on photo.xaml page.
how can i achieve this. I am new to MVVM.Please Help.
Thank you.
You should use messaging center for this
First get it method registered as :
MessagingCenter.Subscribe<YourObjectClassComesHere>(this, "Any Message or empty string will be okay", (Obj) =>
{
//Code you want to execute
});
After this you can invoke it from another page as
MessagingCenter.Send(YourObject(of type "YourObjectClassComesHere"), "Any Message or empty string will be okay");
Hope it helps.
More details are available at : https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/messaging-center/
You can try MessageCenter https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/messaging-center/
In Phonepage you subscript message and send a message from another page.
I Have photo.xaml page in which i have one 1 ListView whoose bindingcontext is ObservableCollection listphoto of photos which is defined in its viewmodel.cs file.
Firstly it would be worth showing your XAML code.
You say in the quote above that you set the bindingcontext of the listview to the collection. You should be setting the ItemSource property of the ListView to the collection.

Durandal navigate to a view with model as parameter

I want to navigate to a view html file (from another view's js file) but pass in the model to be used by that view. How can I do this please.
I can generally do this when I am opening the new view in a dialog. In this case, I create the model object (var model = require('viewmodels/modeljsfile), and then creating an instance of this model and accessing the properties (var instance = new model(); instance.property1 = 'Test). I then pass this instance to my modal.
Instead of opening in view html in a modal, I want to redirect to the actual view. How can I do this please?
In the view add a Compose and state the target view/model. Note that both the View/Model can be variables from the parent view.
Be careful however as not all the life cycle events are attached (activator.js)
<div data-bind="compose: { model:someModelProperty, view:someViewProperty }"/>
http://durandaljs.com/documentation/Using-Composition.html

Is it possible to add a view model on the _layout.cshtml?

The scenario is that I want to populate the navigation bar, the menu, with Mvc.Sitemap.
How should I achieve this?
It feels strange to add a View Model onto the layout page. And that may cause a view to have more than one View Models.
can any one help? Thank you.
Update: ----------
I have Home/_Menu as a partial view which uses MenuViewModel. But how can I include that View in _layout?
I cannot simply use #Html.Partial("_Menu") inside my _Layout.cshtml because I need to specify the controller and action where the view model is built. What is the correct way to do this?
And if I do #Html.RenderAction("_Menu", "Home"), I get a Object reference not set to an instance of an object. error on the Model.
You should not have a model in your Layout, because then every view that you call from there by default will have this model. Instead you should create a Menu view with the model, and when you call it pass a new MenuModel to it as a model.
Edit:
You can have a class that will pull data out of where you store your menu values. Then you call your partial like this:
#Html.Partial("_Menu_", menuobject.Root)
And the class:
partial class menuobject
{
public static menuobject Root
{
// MenuRoots are all roots that have ID -1 - which will be the root
return MenuRoots.Single(x => x.Something == "Topnavi"); // this will return the root above all your menus
}
}
Than in your view you go foreach() on each level of menus to populate them.

Inject a statement from the Controller

I'm writing an MVC 3 app and I have tried to code a control in the Controller (due to permissions, different menu items will be visible for different users) and use the object in the Razor page. For example, in the Controller I do something like:
public ActionResult Index()
{
var menu = "#(Html.Telerik().Menu().Name("menu").Items("menus => { menus.Add().Text("Home").Action("Index", "Home"); menus.Add().Text("Deliveries").Action("Index", "Delivery"); }))";
var model = new MenuModel()
{
Menu = menu
};
return View(model);
}
And in the View I try to render the Menu using #Model.Menu but I just get the string value rather than an actual menu. Is what I'm trying to do possible?
Extend the HtmlHelper class and use the newly created method to render your menu in the View:
Helper:
public static string RenderMenu(this HtmlHelper html)
{
var menu = new StringBuilder();
/* ... menu rendering logic ... */
return menu.ToString();
}
View:
#Html.RenderMenu();
Nevertheless, it is fine to put this logic in the view. Using the HtmlHelper extension just separates/cleans the code.
I wouldn't want to do it that way even if it were possible!
You should decouple your controller and view more than you are currently doing.
The controller should only pass data the view requires. If the view needs a menu with different menu items then use the controller to decide what menu items the view should have then add them to a list object and pass that list to the view. The view will then build a menu based on the list of menu items.
Also when I say "menu items" I don't mean markup! I mean create a new MenuItemViewModel object to persist your data between your controller and your view e.g pseudo code:
public class MenuItemViewModel { string url, string text }
List<MenuItemViewModel> menuitems ...
return View(menuitems)
Why would you want to do this? This is breaking the separation of concerns in MVC - your controller shouldn't worry about how the menu is displayed, just getting the right data to the view for display.
If you want to do security trimming, don't pass in a builder string. There are other methods available.
You could try the MVC SiteMap provider which can handle security trimming against the [Authorize] controller attributes (bit of work to learn and setup, but great once its there).
Pass in your own collection of flags or pre-build menu items, like what Greg suggested.
Make a HtmlHelper extension, something like 'IsAuthorized()' that will check against the controller authorize attributes. Here's a gist of one I used to use before switching to the MVC Sitemap.
With the html helper, you can do this:
#(Html.Telerik().Menu()
.Name("Menu")
.Items(m =>
{
#* Publicly Accessible Controller *#
m.Add()
.Text("Home").Url(Url.Action("Index", "Home"));
#* Secure Controller *#
if (Html.IsAuthorized<MyProject.Controllers.SecureController>(c => c.Index()))
{
//m.Add()....
}
.....

Ignoring viewDidAppear when dismissing a modal view via a delegate

I have a delegate on my modal view controller which activates the following on my parent view
- (void)userTappedOnBackButton
{
[self dismissModalViewControllerAnimated:YES];
}
This successfully dismisses the view.
On the main view's didAppear, data is loaded and a table is bound.
How can I inform the view not to reload this data if the view is displayed via this route?
Set a flag in the parent view controller like self.hasModalOnTop = YES before presenting modal view controller. And check this value in viewDidAppear like this.
if(!self.hasModalOnTop) {
// load data and bind table
}
self.hasModalOnTop = NO;
A little bit hacky, but should work.

Resources