Selected Index Change event of dropdwonlist in MVC4 - asp.net-mvc

I am very new to MVC and Presently I need to work on a Project in MVC4 with Razor engine.
So can any one tell me about
How to implement logic for SelectedIndex Change event of dropdwonlist in MVC4?
Thanks.

In MVC you can't do this. You'd need to use jQuery. OnSelectedIndexChange is a concept that is used in ASP.Net, and MVC3 is vastly different. If you want to implement logic when a user changes a DDL, you'd need a jQuery function to run when the DDL changes (onchange html attribute), which would then pass a request with AJAX to a controller on your page. Within this controller you'd be able to implement this logic.
I'd recommend getting familar with the concepts of MVC first. Practises in ASP.Net are rarely interchangeable with MVC.

Related

if asp.net mvc doesnot support page load event then what should we do for page laod event in mvc

I am new to ASP.NET MVC .
To start learning on MVC, I was going through a tutorial on MVC, where they used the Page_Load event which is same as in Web forms.
Does MVC support events, and if so what events are supported?
If ASP.NET MVC does not support Page_Load event, then what is the alternative?
One thing that i was told before learning MVC was, 'Just forget everything about Webforms. It's old, and doesn't support separation actually.' Don't think about asp.Net webforms while learning MVC. MVC has a different life cycle.
An answer to your question is, MVC doesn't support Page_Load or any event, and it all depends on what you want to implement. Here are few tips:
1.) If you want to implement something before View (UI or html page) is rendered, you can write logic in controller before returning the View.
2.) If you want to implement something in the process of rendering page, you can use razor markups. It is quite easy to use razor.
3.) If you want to implement something after Page has been totally rendered, you can use jQuery's document.Ready().
You can start learning MVC here: http://asp.net/mvc
You should use Javascript/Jquery for event handling in MVC application. You may be use document.Ready() event handler to trigger the page load event and then handle your logic using javascript/jquery.

Porting ASP.NET (Telerik) multi-column combobox to MVC.

I need to port the functionality of this one and only one AJAX control to MVC, but given the poor selection of MVC controls out there, I think I need to bring this legacy control into the MVC world...
I'd rather not taint my MVC project with ASP.NET controls, and welcome json/MVC alternatives you know of. (post them if you know of any)
Sample UI that I need in ASP.NET MVC
Back to porting
Although it's unfortunate that I am left to porting this control to MVC, it seems to be a widely accepted practice since Telerik has detailed instructions on how do this.
That makes me ask:
How common is it for a MVC website to use ASP.NET controls?
Again I'll mention I don't want to do this so I welcome MVC-specific alternatives. That being said, I'll proceed with trying to merge that control with my existing site. </End Disclaimer>
If you click on this hyperlink, and look at the source code at the bottom, can you tell me where I should put the following in MVC?
Code behind (My first instinct is to use a Controller but another SO question indicates I should create a create a ViewName.aspx.cs file)
How do I port the SQLDataSource to the new "Model" way of thinking. I know they are different in nature but I don't know how to present data to a ASP.NET control in a way that it will consume the information.
How do I handle the AJAX component? This control has an AJAX component using callbacks. Yes this is getting ugly, but it seems like I have to do this.
Apparently this model saves data in session or view-state. I have no idea if this even work in MVC. Guidance, an alternate control, or a life preserver is much appreciated.
I've already done research and have instructions from Telerik here and here that describes how to get started with placing a simple menu, but I need a little assistance with the more complex controls like this one.
Note: For all the commentary that has hit this question, please remember that I only want this one ASP.NET control functionality; I can't find a comparable control in MVC.
porting from asp.net webforms to MVC is a paradigm shift.
Directly porting does not work.
The Model is where you typically describe your data and do the data access
the View is for displaying the data
The controller plums the other two together
So SQLDataSource is your data access layer and would therefore go to your model
the problem with the thought pattern of SQLDataSource == Model then you get away from the point of decoupling your presentation from data access
You have to think of MVC development as a new build
I would pick a book or video series from your preferred source and learn starting with MVC3 (it has some differences that simplify build speed and reinforce the difference between webforms and mvc)
Hope this helps.
This article explains how to run web forms and mvc together
http://weblogs.asp.net/rajbk/archive/2010/05/11/running-asp-net-webforms-and-asp-net-mvc-side-by-side.aspx
This is by telerik and explains the limitiation of the grid and what is need to get it to run.
http://blogs.telerik.com/aspnetmvcteam/posts/08-11-06/asp_net_ajax_controls_in_asp_net_mvc.aspx
Add an IFrame in your MVC view that just shows the WebForms page (or just use that control on a single WebForms page).
There is nothing that says you can't have a site with both WebForms and MVC pages. You can route a single URL to a WebForm just for this control.
Why not just use the telerik MVC controls? They work quite well. Either get them via a NuGet package or visit this link http://www.telerik.com/products/aspnet-mvc.aspx
I would rather use ViewModel instead of code behind
You don't have to throw away SqlDataSource you can use result set and buld from it your model, problem may be column names in result set... tricky but can be done
Since there is no components in MVC except helpers youll need help of jQuery probably, it easy
$.ajax({
url : "/controller/action",
data: { /*json or serialized form */ },
successs: function(data){
//if you got response as html from /controller/action
$("#some_div").html(data);
}
}
Session is available in MVC but viewstate not, you can use HttpContenxt.Cache or TempData if you need something like viewstate. USe TempData to keep data between redirections, or httpcontext.cache to cache your data further more.
I can't find similar functionality in an MVC control
MVC doesn't really have a concept of controls in the same way that ASP.Net does - there are only really the plain old HTML controls (i.e. hidden input, text input, checkbox, radiobuttons, select box, text area, password and buttons).
When you need something more complicated than the plain HTML Controls you need to use some JavaScript to achieve this.
I'm not sure that you will be able to 'port' the control into MVC - you will most likely have to try and re-create it your self using an MVC controller and a partial view with a fair bit of a javascript to create the control.
Have a look at the JQuery UI Autosomplete plugin - you could probably use this to acheive something similar

Use an MVC Master Page in a WebForm page

I have an MVC application that I am creating that I have to integrate regular WebForms into the site. The WebForm pages will be using a ReportViewer control so I believe that won't work in a regular MVC view because ReportViewer uses ViewState, etc.
Is it possible for me to create a regular Web Form that uses an MVC View Master page? If so...is it possible to use the Html helper methods such as RenderPartial?
Is it possible for me to create a
regular Web Form that uses an MVC View
Master page?
I don't know for sure, but it might work, as long as it's just markup and doesn't use any MVC-specific features.
If so...is it possible to use the Html
helper methods such as RenderPartial?
Nope. If you use it this way, the Html property will not be set automatically, and I don't know of any way to hack it in.
We began a project in WebForms and realized partway through that MVC suits our purposes far better, so until we can finish migrating away from WebForms we have to maintain two separate versions of each of our "portal" elements (tabs, logout buttons, etc.). It's a pain, but it's doable.
Instead of using RenderPartial you could call a MVC controller with ajax from your webforms app and use the html it returns.
if (Request.IsAjaxRequest())
return View("someascx", model);

Is it possible to have a ASP.NET MVC MasterPage that has webforms User Controls which cause PostBacks?

I'm pretty sure the answer is going to be no here, but I just want to be sure. As you can probably guess I'm in progress of converting a web forms project over to ASP.NET MVC. Thus, I have a web forms master page and a MVC master page. The user controls (.ascx) render fine in the MVC Master page, but the post-backs essentially do nothing.
The best solution I know of is to have partials instead of user controls for the MVC master page. And then have an abstract controller that any controller that uses the MVC master page inherits from. Not exactly DRY, but it's the only thing I know of at this point. Any better ideas?
Thanks!
Darren
Controls to postback such as submit buttons would work fine; anything that renders __doPostBack would not work, such as <button>, <a> (or LinkButton), <input type="button" />, etc.
Also, not sure of all the context, but consider the Html.Action MVC 2 syntax (if using MVC 2) as a way to achieve it too, which may be better depending on what's going on within the control.
You're correct - postback from a user control just won't work in a Mvc environment.
What I have done in a similar project is to gradually migrate the UserControls over to Partials called from MVC or from within webforms. [shameless plug] I have written about how to call RenderPartial and RenderAction here: http://www.blog.clicktricity.com [/shameless plug]

Why should I not use an ASP.NET datagrid control in MVC

I don't want to make this into a "which is better... MVC or WebForms" type of question. Im sure that has been asked a bunch of times here already.
My Question: If I am building a MVC web project, why should I not use an ASP.NET datagrid control on one of my "Views" .aspx pages?
The control relies on Viewstate which isn't available in MVC. In addition, all the behavior is predicated on the WebForms event and postback model which you'd have to recreate in MVC to get them to work. You could search for alternate, MVC-friendly grids (perhaps jQuery-based). I know that Telerik has released a set of MVC controls under open source that might be helpful to you.
you can't use web forms controls in MVC because they depend on view state. Use the data grid of the Mvc Controls toolkit instead. It has paging, insert delete and update capabilities and it is completely templated (you can shap it as you like). Look at it here
The datagrid control depends on postback events which does not fit into the MVC way of thinking (the postback goes to the page code behind rather than the controller). You could use one without any postback features, i believe, but you may as well craft something directly.
Because the ASP.NET DataGrid / GridView has too much responsibility to fit into the MVC pattern. You'll have to add some code-behind to your view to databind the grid - code that belongs in the controller.
Anything that uses postback won't work with MVC, so the DataGrids paging and sorting won't be any good to you. So there really isn't any benefit to using it.
If you're looking for a flexible grid 'control' (MVC prefers the term HTML Helper), the MvcContrib grid is pretty good.

Resources