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
Related
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
I am new to the ASP.NET MVC 3.0, trying to popup partial view using Jscript/AJAX. Any help is appreciated.
Thanks,
Srini
You can launch a popup window with either javascript or jquery. The popup then points to controller/view so that view is displayed in the popup. If you google opening a popup you'll find lots of resource. Its a client side action so really does not have much to do with mvc. You'll just show your view inthe popup.
You can see this article on how to attach code to launch a popup. This is for webforms but the javascript is still applicable for mvc
Also this SO post
Let's say you create a controller action that returns your partial view, something like this:
public ActionResul get_partial_view()
{
....some logic
return PartialView("partial_view_name");
}
Then in your view where you would like the popup to appear you could use some jquery to load the dom element that will contain the content of the popup window like so:
$.get('/controller_name/get_partial_view', function(html) {
$('#popup-content').html = html;
});
I'm working on an Asp.net website (it's been awhile) coming from an MVC background. How would I mimic partial views in Asp.net? Do I use web user controls or web part zones, or other?
ASP.NET is actually the base engine for both ASP.NET Webforms and ASP.NET MVC.
If you're used to MVC then you should really use ASP.NET MVC, where Partial Views are first class citizens.
Use ASP.NET MVC
To use one, you simply create a new MVC project, right click on the shared folder in the View folder, and select Add New View. From there you can select Partial View.
If you have to use Webforms
If you're stuck with webforms, then you use UserControls. The webforms model doesn't really translate well to the MVC paradigm, but UserControls are the closest things.
I think webparts are for Sharepoint only... ignore them.
User Controls are the ASP.NET WebForms equal of ASP.NET MVC Partial Views.
I would use Web User Controls. As far as I can tell they offer the same functionality. Plus the MVC implementation in ASP.NET uses them for partial views as well.
I assume you are using ASP.NET webforms and not ASP.NET MVC or this wouldn't be an issue. In webforms you can use UserControls (.ascx) to achieve a similar result.
One of the advantages of using user controls is you can interact directly with the details of the user control by exposing public properties and methods in the user control itself. You can make the user control have a lot of flexibility and have much of it's functionality defined by pure markup as well.
With partial views in MVC this is not really possible. I can't really think of anything you can't do with a user control that you can with a partial view in MVC. On the flip side there is ton of stuff a partial view can't do that a user control can.
Example of using a user control in a webforms project.
First add a reference to your user control at top of aspx page:
<%# Register Src="UserControls/PersonSearch.ascx" TagName="PersonSearch"
TagPrefix="uc1" %>
Then when you want to use it:
<uc1:PersonSearch hid="ucPersonSearch" runat="server">
</uc1:PersonSearch>
You can register events from you main page with the user control, expose properties, methods, etc...
For instance you could have a user control and register a notify event from your main page with the user control to get triggered once a person is selected. The user control just has to know about checking to see if a delegate has been registered and call it. This gives the container the ability to use the user control however it wants and leverage it's strength in unique ways.
It's more like a partial view on steroids but I 'partial' to user controls (pun intended) :)
I'm fairly new to ASP.Net MVC 2 and understand the MVC pattern in itself. But my question is what's the best way to populate dropdownlists in the UI sticking to the MVC pattern. Should I be going through the controller?
Every article I've seen to do this shows how to do it using javascript and jquery. I have a test application that I'm re-writing in MVC2 I have my dropdowns working with jquery basically calling a WCF Data Service that returns JSON which populates the dropdowns. Seems to me though that this is bypassing the controller and going straight to the model therefore strictly violating the MVC pattern.
Or am I missing something obvious here. You thoughts or best practices would be greatly welcome here.
Thanks
One of the great things about MVC is that the controllers can couple as 'web services' or sort. Meaning, you can easily specify a return type of 'JsonResult' for example (instead of a view - ActionResult).
The MVC framework will handle all the serialization for you.
You can easily call the controller action method from jQuery and populate the dropdown.
In your example, i would create a Json controller method, decorate it with some custom action filters (check http headers that its a json http get request, etc), call it from jQuery and bind to your dropdown.
If your drop-down list is static (i.e. not a cascading drop-down list) then you can add an AvailableItems property to your model, set its value in your controller, and populate the list from that. If your list needs to be updated based on other user selections then you need to call back to an AJAX service of some type.
In general, if your application has script code that runs on the client, that code is going to be in your views. I personally don't see that as a violation of MVC.
I think your best bet is to give View Models a try.
You can build fill out data for special UI oriented models in the controller and pass that to the view. For drop downs, is there a reason you're loading through ajax? In most cases I've found you can just build a normal select list and sprinkle with javascript for dynamical functionality.
Your view model could have a IEnumerable<String> CityNames property that you then load into a dropdown in the view.
can any one guide me.
Post form data to controller without page refresh in asp.net mvc application using Jquery ajax.
There are some examples here. What part are you having difficulty with? You really have to go out of your way to make the page refresh when using AJAX.
Replace "IActionResualt" with "void"
public void Create(...)