All Viewmodels Inherit From 'BaseViewModel', Can Set This Up In OnActionExecuting

Jan 22, 2011

If all my actions have a model that inherits from BaseViewModel, is it possible to initialize this model from the OnActionExecuting method?

Currently in all my actions I do this:

var model = new SomeModel();
model.User = Users.Get(...);

Now I am loading the user object in OnActionExecuting, so I was hoping I could setup my model from there somehow.

View 1 Replies


Similar Messages:

How To Get Current Route Information In OnActionExecuting

Jun 3, 2010

my urls look like:

www.example.com/{languagecode}/{controller}/{action}/{id}
where language code is en-us, etc.
From the OnActionExecuting event, how can I get these values?

View 1 Replies

C# - Redirect In FileAttribute's OnActionExecuting Not Properly Working

Feb 3, 2010

I have an action called EditProfile. To secure it I have added a class RequireUserLogin inherited from ActionFilterAttribute. In the OnActionExecuting, when I redirect user to login page, before going to login page, it first execute the EditProfile action code (which i don't expect) and than redirect the user to login page. I want to not come in action code. Currently the only option I have is throw exception. Is there any other options. The code is:

public class RequireUserLogin : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (string.IsNullOrEmpty(userID))
{
filterContext.HttpContext.Response.Redirect("http://localhost/test/login");
}
base.OnActionExecuting(filterContext);
}

The EditProfile action is:

[RequireUserLogin()]
public ActionResult EditProfile()
{
}

View 1 Replies

ViewModels Versus Presentations, Or Both?

Apr 28, 2010

Previously to learning about ViewModels and AutoMapper, my infrastructure project had the following classes defined:

Csharp Code:

[code]....

View 2 Replies

How To Get ID Of EditorFor With Nested Viewmodels In Mvc 2

Mar 30, 2010

So I have two nested view models, CreditCard -> BillAddress. I have a view, "EditBilling", that has EditorFor(CreditCard). The CreditCard EditorTemplate has EditorFor(BillAddress), and the BillAddress EditorTemplate has EditorFor(BillState).

The end result is a select list with id "CreditCard_BillAddress_BillState".

I need to reference this in javascript, thus need to know the ID. In other situations, with non-nested ViewModels, I have used the following code:

$('#<%= ViewData.ModelMetadata.PropertyName %>_BillState')

The problem here is that the ModelMetadata.PropertyName property is only aware of the current property, not the parent(s). So I end up with the following:

$('#BillAddress_BillState')

How does one go about getting the client ID of nested strongly typed helpers?

View 1 Replies

Duplicating Properties In ViewModels

Nov 25, 2010

I have a question regarding the duplication of properties within view models. For my Search View i have a viewmodel that looks like this

public class SearchModel
{
public IEnumerable<SelectListItem> Genders {get;set;}
... other select lists
// Worker Details
public string FirstName {get;set;}
public string LastName {get;set;}
public DateTime Birthdate {get;set;}
public int Phone {get;set;}et
public string Gender {get; set;}
//Address Details
public string Street {get;set;}
public string City {get;set;}
public string Zip {get; set;}
}
For my Input View i have the following View Model
public IEnumerable<SelectListItem> Genders {get;set;}
public IEnumerable<SelectListItem> Directions {get;set;}
... other select lists
// Worker Details
public string FirstName {get;set;}
public string LastName {get;set;}
public DateTime Birthdate {get;set;}
public int Phone {get;set;}et
public string Gender {get; set;}
public string SSN {get; set;}
public string DL {get;set;}
//Address Details
public int Number {get;set;}
public string Direction {get;set;}
public string Suffix {get;set;}
.....
public string Street {get;set;}
public string City {get;set;}
public string Zip {get; set;}
}
List Display Model
public class ListDisplayModel
{
public IEnumerable<Worker> Workers {get;set;}
internal class Worker
{
public string FirstName {get;set;}
public string LastName {get;set;}
public DateTime Birthdate {get;set;}
public int Phone {get;set;}et
public string Gender {get; set;}
public string SSN {get; set;}
public string DL {get;set;}
//Address Details
public int Number {get;set;}
public string Direction {get;set;}
public string Suffix {get;set;}
public string Street {get;set;}
public string City {get;set;}
public string Zip {get; set;}
}
}

I feel like i'm duplicating a lot of properties. I was wondering if it would be ideal for me to go ahead and create like a DTO class called worker and just place it in each of these view model classes or is there a better way to do something like this?

View 2 Replies

MVC :: ModelBinder For Dependency Injection On ViewModels?

Jan 25, 2011

This project is pretty far away and I'm not in the position to go make changes all over the place (If I could, deleting the lot would be what I'd do!)

I want to create a modelbinder that would resolve any dependencies my View Models might have (using StructureMap).

It should not require me to implement a specific interface (so many developers, so many interfaces..I rather keep things clean) and hopefully not require one to go register each model binder individually (Now I'm asking too much,taking the first requirment
in consideration).

Probably will get it right tonight, but figured I'd ask.

View 3 Replies

MVC :: ViewModels And Updates And Where To Create Gui Elements?

Mar 21, 2011

I have relates to how we update using a view model. I am trying to pin down whether a viewmodel is disposed once a View is rendered to the browser or whether the viewmodel continues to exist even after the View has been rendered.

Essentially I have in my viewmodel the following code:

[Code]....

This is used to bind html elements in my view so if I need to make a modification to a Item contained in the list can I simply access this and post the VM back to the controller or do I need to post a Item object back to the controller in the singular. Secondly where is the best place to build a gui control such as a table. If in the view I have a foreach loop and write out table fields is this the most appropriate way of achieving this or should I deal with this in the Controller or Model and pass it to the gui in the View Model?

View 1 Replies

MVC :: Controller Base Class Initialize() And OnActionExecuting() Not Called When Unit Testing?

Nov 16, 2010

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.

View 2 Replies

How To Set A Property In Custom Controller Wrapper In OnActionExecuting How To Pass To Site.Master

Jun 3, 2010

Say I have a class that wraps the Controller class:

public class MyController : Controller
{
public string SomeProperty {get;set;}
public override void OnActionExecuting(...)
{
SomeProperty = "hello";
}
}

Now in my site.master, I want to have access to the SomeProperty that I just set. How can I do this?

View 2 Replies

C# - Pass Nested Lists From View To Controller Using Viewmodels?

Mar 24, 2011

I am Francesco and I am newbie. I have a problem with ASP.NET MVC3 regarding the data posted between View and Controller. My goal is to use a form to edit a specific record from my database and then post it back to the controller, which is in charge of checking the record's fields and submit them into the database.
The data are passed from the Controller to the View by using a ViewModel and therefore the DefaultDataBinding does not apply in this case. I tried many approaches, I monitored the communication with Fiddler but I couldn't solve my problem. The method described by Phil Haack does not work and neither does the FormCollection.

Here you have the code:

ViewModels: I pass it to the View to create a table. Name and the list of Item names (table columns headers) are just to be displayed. I want to have back the Id, the Month (they are passed correctly as parameters and represents table row headers) and most important the ActualValue of the Items and their Id (I have an association table in my database with Id, Itemid and Month as keys)

[Code]....

View: The View is strongly typed as ViewModels.EditViewModel and sends the fields through a submit button. I have to generate a table by using foreach loops, because I do not know how many items there are in advance

Controller: The controller has to update each kpi passed from the view related to a specific Id and Month.

[HttpPost]
public ActionResult EditMonth(int Id, int Month, //FormCollection kpiValues)

View 1 Replies

C# - How To Inherit From A Page

Feb 8, 2011

I created a page to be the base Page in my web application to add inside it some common functionality. but when I try to inhirite from it then VS gives me the following error:

The type or namespace name 'CentralPage' could not be found(are you missing a using directive or an assembly reference?)

Note: I just want code-behind functionality in the base page.

What should I do ?

View 2 Replies

MVC :: Inherit From DefaultModelBinder?

Mar 28, 2010

What method to override in order to be able to capture the data via a custom modelbinder. I noticed this article:http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC__Part_2.aspx and there GetValue is overridden but I don't see that in MVC 2. What must I do to be able to make this work?

View 2 Replies

Want To Inherit Bill Class?

Sep 2, 2010

I want to inherit more then one class is there any method ?For instance in login.aspx page:

<%@ page language="c#" codefile="nishant.aspx.cs" autowireup="true" inherit="nishant"%>

now code behind file

nishant.aspx.cs:

class nishant
{
//code...
}

class bill
{
//code.....
}

now i want to inherit bill class then how i will ?

View 5 Replies

Inherit Data From One Class To Another

Feb 10, 2011

How does we inherit data from one class to another class using Inheritance in practically using c# .net show me some examples. My doubt is we create one method in class and we will call that method when ever it is neccessary in same class , But if we want to call the method and use that properties in another method in another class what is the process?

View 4 Replies

Inherit Same Method For 2 Different Class?

Dec 19, 2010

How to simplify this, 2 method in class are identical. First class.

Public Class Gui
Inherits System.Web.UI.Page
Public Sub CreateStyleLink(ByVal StyleArray() As String)
Dim StrStyleLink As String

[Code]....

View 3 Replies

VS 2005 Inherit From Windows Control?

Jan 27, 2010

I want to make a user control in asp, can i inherit from a windows control to do it? the book i have says i can inherit from any .net control, so i dont see why not but want to be sure

View 4 Replies

MVC :: How To Inherit Two Classes Using Page Directive

Aug 10, 2010

I have been trying to find a way to declare two classes using page directive, but it seems like mvc framework doesnt support it.

I have got :

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<ContactManager.Models.contact>>" %>

which shows the list of the contacts including edit delete and create option.

What I need to declare another model class which is FileRepository.cs for uploading files on the same page. In this case the page directive should be like:

2. <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<ContactManager.Models.FileDescription>>" %>

Is there any way I can incorporate both into one as the first is coming from the database.

View 6 Replies

C# - Inherit From Control And Create Custom One?

Oct 22, 2010

Im inheriting from GridView control creating MyGridView class which has cs extension, not ascx. U put it in App_Code folder and in web config added:

<add tagPrefix="mgv" namespace="MyProject"/>

it works fine but when I wanted to convert it to web site project I have problem that it cannot find this reference.

View 1 Replies

AJAX :: How To Inherit From An Exisiting Controls

Sep 28, 2010

Is there any normal way how to extend existing ajax control?Situation: I have 1 ajax control named AjaxControl1 that has AjaxControl1.js to define javascripts. There is string property Value1 accessible from client.Now i need create new ajax control named AjaxControl2 inherited from AjaxControl1. all server and client functiouns are same plus i need add string property Value2 accessible from client too. but i dont want create copy of AjaxControl1.js and add some new rows, but use existing javascript file AjaxControl1.js and somehow extend from it to get access Value2 from client.Point is what if i change something in AjaxControl1, i want that change in AjaxControl2 too but without change in AjaxControl2 or AjaxControl2.js

View 3 Replies

MVC :: Inherit More Than One Model In A Single View?

Jan 6, 2010

i am using more than one table.i created linq to sql for database model.in that each table is a different class or model whatever.when i m creating any new view i inherit one model or table.but when i want to use more than one table hoe can i implement this.

View 3 Replies

Inherit The Apperance And Layout Of The Masterpage?

Jan 11, 2011

I have a masterpage that uses an external .css file to set the masterpages apperance across the site. Now I have eight pages on my site and I use the masterpage for seven of them. I have a single page I do not wish to inherit the apperance and layout of the masterpage and so was wondering how would this be done?

View 6 Replies

Web Forms :: How To Inherit Properties To Webpage

Nov 3, 2012

I want to set useraccess permission for webforms. I want to have a common class say userClass for that function where privileges are set for e.g. (canAdd,canDelete,canEdit etc). From the webform I need only to check the properties such as canAdd, canDelete, canEdit are true or false. According to that privileges are set.

I dont want to call the userClass function on every page load. It should be automatically called when the page gets loaded.

View 1 Replies

C# - Can Two ASPX Pages Inherit The Same Code Behind Class

Sep 4, 2010

I'm just starting out learning ASP.NET. From what I understand, ASP.NET differs from old school ASP in that the logic code for a page exists in as separate file rather then being embedded in the ASP page. So when a user requests a page like ShoppingCart.aspx the server reads the directive at the top ...

<%@ Page Title="" Language="C#" MasterPageFile="~/Styles/Site.Master" AutoEventWireup="true"
CodeBehind="MyShoppingCart.aspx.cs" Inherits="TailspinSpyWorks.MyShoppingCart" %>

This tells the server what file and what class in the file is associated with the page. The code behind class also has member variables that correspond to each control on the page, and provide a way for code in the code behind file to manipulate the controls.

First, do I understand this correctly?

Second, could a site be setup with two separate ASPX pages with identically named controls, which both had a directive pointing to the same file and class? Would you even want to do this? Both pages could have the same functionality but a different layout. I was thinking this could be a way to create separate "desktop" and "mobile" versions of a page with out duplicating content in the code behind files.

I guess ultimately what I'm wondering, is if there a way to define an abstract page? Say create an abstract page definition that says a page must have controls "cart_list", "total_lbl", but then be able to have multiple pages that inherit from this abstract page?

View 5 Replies

Is It Possible If Inherit Aspx Page From System.Web.Mvc.ViewPage

Apr 4, 2011

I am developing a website which will be having both asp.net pages and MVC pages in it, So I have BaseWebPage class which will be used for both asp.net pages and MVC Views. but My BaseWebPage class is inherited from System.Web.Mvc.ViewPage, So Will there be any code/ functionality break for normal asp.net pages, because System.Web.Mvc.ViewPage is overriding some of the Pagelife cycle methods.

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved