MVC :: How To Create Class In Controller
Sep 18, 2010I am using vs 2010, mvc, linq to sqlMy Model is .dbmliletbl_teacher,tbl_class,tbl_subject etc are involvedI write code in my controller for table join.
[Code]....
I am using vs 2010, mvc, linq to sqlMy Model is .dbmliletbl_teacher,tbl_class,tbl_subject etc are involvedI write code in my controller for table join.
[Code]....
In My Project I want to Create a Class Library Contains a Controller based on its module, Example : Class.Controllers.Album,Class.Controller.Artist
in this Class Library I wrote two Controllers AlbumController.cs, ArtistController.cs.When I add this to Main Project Contains Views,
my MVC2 delete and only my delete controller is refusing to return any class information. Its really similar to my edit function and the views are all auto-generated so I don't see the problem.
Function Delete(ByVal id As Integer) As ActionResult
How do you get the current action / controller name in a controller or class?
i can't show it in my view but that's not what i want.
I've been working on a rather large web application that requires a specific id param in the url for every page visited (for example, /controller/action/id?AccountId=23235325). This id has to be verified every time someone visits a page. want to reduce code replication as much as possible, and was wondering if there is a way to use an init method or constructor in a controller that inherits the MVC controller, and then have that controller extended by the others.
View 1 RepliesI have a facade class that my controller class calls out to and in that facade class, for whatever reason, I'm building a string a href element. And it resolves fine in casini but when it gets out in the real world like localhost it doesn't work. Is there a way to do this?
string goBackLinkForErrorMessage = "<br /><a href='/MyController/Action?id=" + blah + "'>Go Back</a>";
Hi I have created the following controller i want to add view that should have the data access class UserMagnament.Controller.menuitems but when i add view by right clicking on view folder,it is not showing "UserMagnament.Controller.menuitems" in add data class
No controller is shown such as homecontoller, accounts controller, UMController(My Controller) etc i have converted my solution from mvc 1 to mvc2. i checked the web.config. it seems ok for MVC2
[Code]....
How to pass the textbox value to my controller there is no form and post method in the viewpage.I have taken one input type textbox which is not bind with my model.I need to access that value to controller
View 13 RepliesIs there any way to convert our class to controller in asp.net MVC ....
View 1 RepliesI am creating an ASP.NET application that allows users to edit and insert data that is persisted in a SQL Server database.I would like to implement a controller class that handles queries for certain entities, caches results and triggers events when data is inserted or updated.I need one of these controller classes for each entity in my data model.What I'm unsure of is how to create a singleton class of this sort that different user sessions can access.
For example,I have a Employee entity in my data model.I would like to have an EmployeeController class that handles retrieving the employees from the database, sends inserts and updates to the database,keeps a cache of all employees and triggers events when an employee is inserted or updated.Objects in other user sessions could then listen and respond to these events.
Am I on the right track?Should I be using the ASP.NET MVC framework to accomplish this?How do I instantiate these controller classes when the first user needs them and have subsequent concurrent users access the same class?
EDIT:I've been researching the idea of a controller class as an anti-pattern since @TomTom's answer.I'm not convinced one way or the other.I still need an answer to question #3 above.Let's just drop the specific idea of a controller class for now. How do I create any class that is:accessible by multiple user sessions,allows only one instance,is deleted if there are no user sessions,and triggers events that other classes can listen to.
I am having problems adding a reference to a custom assembly in my Bin folder within a controller class I am writing. I can see the dlls in the Bin folder of my Solution file and when adding a new 'using' block to the top of code page, its not picking up any of my assemblies as I try to spell them out.I tried just typing them in and building the solution, but I get errors, it just wont pick these up!
View 3 RepliesI built a base controller class inherits from Controller class to add some custom behavior to all controllers which will inherit from this base class.
My issue is when I try to access HTTPContext and Session objects in my custom base class, they are always null.
Am I doing something wrong or need missing something?
My custom base class definition:
public class ApplicationControllerBase : Controller
controllers classes definition:
public class HomeController : ApplicationControllerBase
using ASP.NET MVC 1, .NET 3.5.
UPDATE:
It seems the issue is I try to access HTTPContext in the constructor while HTTPContext is not ready yet, I tried it in the OnActionExecuted event handler and it works fine.
My question is what is the best place to access Session object in my custom controller class, that will guarantee executing my code with all controllers.
I'm using a simple base class for my controller that sets a piece of ViewData and makes use of the HttpContext. My understanding is that I need to put this code in Initialize() or OnActionExecuting(). I tried both, but believe Initialize() is what I want.
This works when running the app in VS just fine.
I wrote a unit test that calls the action and checks the ViewData for the expected value. When unit testing, neither method is called.
Assume the current executing action method is Index() in Home controller. From within Index(), how to obtain the physical file path of the Home controller? Assume we don't know the file structure until runtime.
View 2 RepliesUML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development, that follows the UP(Unified Process). It uses a Grasp Controller pattern to interact with domain classes by some methods like NewSale(), AddNewItemToSale() and CloseSale. In windows form, I can instantiate a object of this class in the UI and then use its methods to perform the actions. This works well in Client apps, but when I use asp.net mvc, I cannot find a way to instantiate an object (one for each user) that was always visible for a Controller (MVC). I cannot insert as an attribute inside Controller because it always create a new one.
View 1 RepliesI'm very new to MVC so this is a simple issue, just can't seem to find any information on why it's happening so turning to the forums. Edit works, delete sort of works (that's another issue in itself), and details works, but create causes an issue. I've debugged the form being passed in and it's always null with default values. It's almost identical code to my edit so I'm not sure why edit works and create won't. I'd show code for the view but since it's working for edit I'm currently ruling that out as the problem. "Colleges" in this populates a dropdown in the view. Here's the code:
[Code]....
In my controller I fetch a list of object 'object1' which has two fields field1 & field2.Now I have a model model1 which has two fields fieldsX & fieldY.fieldX = field1fieldY = dosomething(field2 also have a view model ViewModel1 which has a single fieldpublic List<model1> model2I need to send model2 to my view
View 4 RepliesI want to link to a MVC View from my code-behind in a Webform, but I can't figure out how to do it and I have googled for an eternity :(
View 3 RepliesShould we create a class for every form we create,that will us to do the validations etc etc?
View 1 RepliesI'm just started with MVC3 and have the following error when I want to run my application :
An error occurred when trying to create a controller of type 'SignInMVC3.MenuController'. Make sure that the controller has a parameterless public constructor.
This is my MenuModel code
[Code]....
This is my MenuController
[Code]....
This is the code of my LoadMenu.asx (view usercontrol)
[Code]....
And this code is in the site.master page where I want to render the menu
[Code]....
I am using Entity Frameowrk as the ORM. I am new to MVC and trying to develop a samll module as a learning project.
Now I am stuck with an issue for which I can't seem to find out answer on web. The issue is :
I have two tables mapped as entities - Tenant_Definition and Contact
Tenant_Definition { Tenant_ID , Desc, Contact_ID}
Contact {Contact_ID, Company_Name,Company_Address,Person_Name, Person_Phone_Mobile}
My View has to present a form that shows Tenant Desc and Contact related fields. The index view works fine and shows the desired values.
But, the create view isn't working out. In the create view, i am planning to do -
1. creating a new contact entity instance and saving it in db
2. Then creating a new tenant entity instance using the new contact_id and save it to the DB.
However, the code doesn't seem to be inserting any value inside Contacts table. It inserts null value.Below is the code snippet of my Create Controller -
[HttpPost]
public ActionResult Create(FormCollection collection)
{
var newContact = new Contact();[code]....
All I want to do is learn how to create a class procedure. And then. How to call it. As an example, A + B = C is the procedure. I would create a class. Right? Then do I name the procedure and put it in the class? Or create a new class for each procedure? Then I would want to, on a local level, substitue values for A & B to return C. So, if the public procedure exists, how to I call it? I know that this is really simple stuff, but I need examples to be simple.
View 1 RepliesI am trying to create a pageBase class where I can dynamically pull data from the database and display it on a web page. Each page I specifically set an ID for the sql query which pulls back the information such as page title and page text.
my problem is that I can't seem to pass the information captured from the query to the page. I get problems such as can't convert string type to literal and I need to declare a new keyword to an object.
PageBase Class
Csharp Code:
[code]....
A instance of a class is created in the partial class of an aspx page.Under page_load or button click method I'm trying to set the value to the class. but when each postback takes place new instance is created and I'm losing the previous value.
public partial class DatabaseSelection : System.Web.UI.Page
{
DBProperties dbpro;
Metadata obmeta;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dbpro = new DBProperties();
}
My ultimate goal is to create a UserControl in a class in my App_Code folder and render the html from that into a string.
The rending of HTML can be done using this example I believe:
How do I get the HTML output of a UserControl in .NET (C#)?
My Question:
I created my user control
public partial class Controls_MyUserControl : System.Web.UI.UserControl
I've registered it in my web.config
<add tagPrefix="UC" src="~/Controls/MyUserControl.ascx" tagName="MyUserControl" />
I can reference this on a page just fine
<UC:MyUserControl ID="MyUserControl1" runat="server" />
Yet when I try to create an object of this control type in a class (in my app_code folder) it doesn't allow me.
Controls_MyUserControl dummy = new Controls_MyUserControl();
The potentially strange thing is when I put this code into a code behind of a page, it works.
what I need to do to have the usercontrol to be able to be created in my class in the app_code folder?
My guess is, is that I need to reference the control in a "using" statement at the top, but I'm not sure what that'd be.
It's not in a namespace to my knowledge (at least there's no namespace, in the code behind of the actually user controls). Though I'm not sure if it inherits one from the System.Web.UI.UserControl.