How to use a Durandal view as a template? - single-page-application

I have a Single Page Application (SPA) developed using ASP .net MVC and Durandal.
I have a view Test.html and corresponding view Test.js.
My requirement is I want to display this view within another view (using as a template) and also display this view for a route - say http://localhost/MyApp/#Test.
Any suggestions on how can I achieve this?
My problem is, in the view I need to return the instance of my view model if I want to display the view for the route. And if I do this, I cannot use this as a template within another view.

You can handle this by using the Durandal compose knockout binding.
<div data-bind="compose: { view: 'views/myview.html', model: object }"></div>
Here you can bind anything you want to object as your view.
See #3 here for more information: http://durandaljs.com/documentation/Using-Composition.html#composing-explicit-models-and-views

Related

Insert a modal razor page in the navbar. How to do it?

I'm building a new website and one of the requirements is to have the login form be a modal window. I'm trying to include it in the top navigation bar and it's only being rendered if the user is not logged in.
How can I add this modal window with it's own model inside the top navbar? Are there any alternatives?
If I delete the model and let an empty modal everything works perfectly but when I add it again it doesn't work, because the model of the page (in this case the index page) is a different one then the one from the modal login.
P.S. I'm using Razor Pages and ASP.NET Core 2.2.
Partial View
So you make a _LoginPartial.cshtml file. and let's say you set #model LoginViewModel
Inside this _LoginPartial.cshtml you have your Login Modal and all the functionality.
Now when you invoke your partial inside an Index page that has model #model AnotherModel, you need to pass a new model to the partial like so:
<partial name="_LoginPartial" model='new LoginViewModel()' />
name is the name of your cshtml page.
model is the #model of the page.
Read More Here
View Components
To be brief if you take this route this is essentially like nesting a little controller inside your page. Allowing you to change scope for your #model.
Read more about View Components
Update for nested objects
You need to instantiate the object property.
<partial name="_LoginPartial"
model='new LoginViewModel { InputModel = new InputModel() }' />

ASP.net MVC fire controller event when view load

I'm using ASP.net MVC and angular js to create a web. I used angular ng-route to render a View when click a link. The moment when render the View, i want fire the Controller event in order to bind the data and display it. (Similar with ASP.net web form, when page load then can call the data binding function).
I'm new in MVC, hope can get some help. Thank you.
As you are using MVC, it would be better if you use the return View(); statement on a Controller, and it would render the view that you want.
Here's a sample:
http://www.codeproject.com/Articles/806029/Getting-started-with-AngularJS-and-ASP-NET-MVC-Par

Is it possible to "pipe" output from one view engine into another?

I was wondering if it is possible (even if it is a long shot) to pipe generated markup from one view engine into another with ASP.NET?
Could a view engine be built to explicitly to support such a thing (given the constraint of the ASP.NET Framework)?
What about Web Forms and Razor?
If you want to include output from one view (Razor) into the another could you create it as a partial view and call render partial in the other.
Even it your other view is not partial you can create some kind of "adapter partial view" that will call #Html.RenderAction() or #Html.RenderPartial() in order to include the first view.

PartialViews in ASP.NET MVC4

I've noticed there are several ways to use Views and PartialViews:
RenderAction, RenderPartial, and "return PartialView"
RenderAction when placed inside HTML, will simply call an Action and Render the View returned (the View returned can be partial view or view?)
RenderPartial will simply retrieve the contents of a View without executing any Controller action
Finally, what's the difference between "Return View" and "Return PartialView"?
Thanks
return View() returns the view with a Layout enabled so you get full HTML page with <html> and <body> tags. return PartialView() on the other hand disables the Layout and you get only the HTML fragment contained in this view. Actually when working with the Razor view engine I prefer to talk about templates and not views and partial views. That's because a view is a template and a partial view is a template without a layout. But in both cases it's a Razor template.

ASP.NET MVC: Basic form question

A friend has asked me to help him with a project that's MVC. This is my first experience with MVC. I'm trying to build the MVC components for a form for him.
A page has a modal popup which uses a JavaScript to POST or GET and receive HTML backā€”that it displays inside the popup modal.
I need to create an MVC form that has validation to display inside aforementioned popup. This popped-up form will be used elsewhere on the site, so needs to be modular.
Should I create an ActionResult in my Route's Controller that returns an View (.aspx) containing my form? Should I instead create a Partial View (.ascx) that has the form, then use that Partial View in a View for my Route so other parts of the site--other Routes--can do the same?
I'm stuck at that basic understanding. From there, I don't even know what to do about the validation (was told the same validation will be used on a nearly identical form) and how a ViewModel fits into this like is used elsewhere on the site.
I've been reading a lot and looking at a lot of examples but I'm still confused.
You've got two separate questions here really. To deal with whether you should use a partial view or a regular view, just think of it this way. Does the HTML content in question represent a full page, or just a piece of a page that will be reused inside of other pages?
If it's the former, then use a full View. If it's the latter, then put it in a partial view. It sounds to me like you just need a full View. In either case, it's easy to change to the other if it turns out it's not meeting your needs.
As to validation, take a look at xVal if you're using ASP.Net MVC 1. It allows for easy validation using attributes on your models.
When you make your post from the modal popup do the validation then in the actionmethod you created specifically for that popup. If you want client side validation write up the js to do it.
As for the modal markup and what not just create a partial view for that, shouldn't be a big deal.

Resources