Questions tagged [asp.net-mvc-4]

ASP.NET MVC 4 is the fourth major version of the ASP.NET Model-View-Controller platform for web applications.

Filter by
Sorted by
Tagged with
8
votes
2answers
12k views

DbSet<T> IncludeAll method

The Problem As an ASP.NET MVC4 developper, I'm using Entity Framework a lot. Considering performance I often use lazy loading for my models. ...
5
votes
3answers
520 views

Recording error messages from an exception

How do I shorten these if else statements? As you can see, those elses are repeated. ...
8
votes
2answers
13k views

Truncate port number from absolute Uri

We had a requirement to remove the port number from the Request.Url.AbsoluteUr i.e Actual: https://mysitename:443/Home/Index ...
2
votes
2answers
114 views

Open closed principle in a method

I have an ASP.NET MVC project wherein I need to follow the open closed principles. The project converts a .csv file to a model from a database, but in the future we might also have to convert Excel ...
11
votes
1answer
3k views

Injecting SelectList objects into ViewData to enable using EditorFor on dropdown based properties

I have developed a system whereby I use an attribute to state which SelectList to use for an 'FK' property. I would appreciate some feedback mainly on by good ...
4
votes
1answer
9k views

Listing users in Azure Active Directory

The code below is the Index Action from a User Controllers. Basically this code returns a list of users from Azure Active Directory using Azure Authentication Library (ADAL). However I need to show ...
7
votes
1answer
11k views

Passing a selectlist to the view based on database models from viewmodel is a MVC anti-pattern

We need to select a model reference from a view. To do this we need to add a dropdownlistfor. This is based on a selectlist containing database models. passing selectlists by viewbag This solves our ...
29
votes
3answers
129k views

Update only modified fields in Entity Framework

I'm working on a website on ASP.NET MVC4 and EF5. I want to ensure that only modified values are updated in the database. I'm using a unit of work pattern and repositories for data work. Here's the ...
2
votes
1answer
140 views

Use of C# Razor Syntax in MVC.net view

I have the code below and I am looking for input as to how I can improve it. The main question here is should the code at the top in razor brackets be moved to the Controller and returned via a ...
1
vote
0answers
37 views

Cancelling long running operation in MVC4

I have been wondering if the way I have implemented cancellation of a long-running import operation is correct. My questions are: Did I handle cancellation correctly? Is it ok to pass the ...
9
votes
1answer
9k views

Multi-checkbox validation

I wrote the following custom validation annotation for my project to confirm that at least one checkbox of a group of checkboxes is checked: ...
7
votes
1answer
18k views

Custom System.Web.Http.AuthorizeAttribute

I've hacked together what feels like a mess for an authorize attribute to secure web api methods. The code appears to be functionally correct; passing the unit tests in place. The code has me worried ...
7
votes
1answer
90 views

Retrieving all institutions in a specified country

I'm checking each variable to prevent null exception, but still wondering can this be improve in much simpler and cleaner if you will be writing the same code? Please see the below snippet: ...
6
votes
3answers
19k views

Configure AspNet.Identity to allow for either username OR email address on login

I am wondering if there is a more efficient route to take here. Using AspNet.Identity I would like to allow the user to sign in to the same text box using either ...
7
votes
3answers
756 views

Ternary extension method

I created the following HtmlHelper extension method for my Asp.Net MVC Razor views because the ternary syntax sucks when you need to use it intermixed with markup. Is there a better way to write this ...
2
votes
2answers
15k views

Calling Web API from MVC generic way of approach

Our Project architecture is AngularJS + MVC + WebAPI. Angular JS would call MVC action and MVC action further calls API methods. Below are some of the action methods we used for calling api methods. ...
1
vote
1answer
13k views

Passing model to _layout.cshtml

I have login page that looks like: It has a ViewModelBase and a ViewModel as: ...
3
votes
1answer
68 views

Changing a div that contains a view link

I've written some code to load a section of my application between views. While my code works, I'm wondering if there is a better way to do it. (partial views perhaps?) I currently load the views, ...
11
votes
1answer
9k views

Generic ASP.NET MVC controller that generates many pages

I recently set out to create an open-source ASP.net MVC web development framework. Specifically, I wanted to automate some of the tasks associated with the creation of data-driven applications. I've ...
1
vote
1answer
397 views

Displaying records from a MySQL database

I've started learning .NET with MVC 4 in C# and I built a basic application to display records from a MySQL database. It's incredibly basic, but it's the best I could come up with being a complete ...
3
votes
4answers
371 views

Website news message edit panel

The explanation I'm making a website for a hypothetical theatre to get a better feel of MVC web-applications in ASP.net. The idea is that there are newsmessages that are displayed on the homepage, ...
1
vote
1answer
279 views

Defining application flow in ASP.NET MVC5

Currently I am assigned to a project which has a standard view-flow, yet this flow is determined by whether settings are set to either true or false. So, for example, let the standard flow be ...
17
votes
1answer
22k views

Using separate DBContext classes

I know that the DBContext represents a session (Unit-Of-Work and Repository) with the database, however I an unsure as to when I should have a different ...
5
votes
1answer
2k views

Implementation Async/Await

I have a simple ASP.NET Web API project. Now I want to make all basic methods as asynchronous. I tried to do something, and it's working but I'm not sure that it was implemented properly. Please ...
3
votes
1answer
408 views

N-tiered web app to save a role

My project manager is saying that there are many bad things in my code, but they are not helping me to correct it. Can you please specify the bad things they mean? I have 7 projects in my solution ...
2
votes
1answer
1k views

Extending the MVC Routecollection

I'm currently developping an MVC framework on which I will be writing my websites. This is done through the usage of a starter kit. In standard MVC, your routes are registered like the following: <...
2
votes
1answer
187 views

Restricting search to a user's own department, if the user is not a manager

I hate having almost two identical blocks do almost the exact same thing. I am working with Entity Framework 4.4.0.0 Asp.Net MVC Right now if the user is a "manager" I want to show them the entire ...
5
votes
1answer
373 views

MVC4 Routes, using Default

Should I leave the Default Route in if it's not adding any benefit? I have a RouteConfig.cs file that looks like this: ...
8
votes
2answers
2k views

Accessing server side variables in views and JavaScript

I was a PHP developer and I started writing a product in C# without studying the basic structure. Now, I have around 100+ views with similar code. After 1 year into development, I now realize that ...
6
votes
1answer
256 views

PBKDF2 authorization

I've discovered that using hashed passwords with salts is a much better idea than MD5/SHA256, so I'm not hashing them with PBKDF2. However, I'm wondering if this is a correct approach to authorizing ...
8
votes
1answer
3k views

Stock list view

I feel like using this many partial views where a view renders a partial that renders a partial is just digging a hole of poor design, so I'm looking for any suggestions or guidance as to if this is ...
3
votes
1answer
370 views

Avoiding redundant code in MVC view page

Based on the true condition, I am making some operations. But I want to simplify the below HTML code in a better way. ...
2
votes
2answers
90 views

Grab set of data from database that matches userid and return it to the view

I want to get the row of data that matches the user ID I have. Then I want to return that view to my view to be displayed in my textboxes for editing. Here's the action that gets called when a get ...
1
vote
1answer
4k views

Creating models manually in MVC

I am creating my models manually. I have five tables in my database as follows: Members MemberTypeMasters ...
6
votes
1answer
131 views

MVC4 approach to checking authorization after POST

I have a very simple form that is designed to update account expiration dates. I'm currently creating a View Model and sending that to the form, however, I still have to pass along a GUID so I know ...
7
votes
2answers
2k views

ASP.NET MVC to produce a repair invoice

I just started working with ASP.NET MVC a few weeks ago, and I'm finding that it can be very easy to write spaghetti code in the controllers. For my first project, I created a very simple view with a ...
2
votes
2answers
10k views

Making a property virtual to cause EF to load the property

I am walking through a sample MVC4 ASP.NET tutorial available on PluralSight.com, by Scott Allen. I am in a section #5, Working with Data (part 2), "Listing Reviews". This application has a database ...
2
votes
2answers
1k views

Selecting security questions and entering answers

I have a dialog that houses 6 security questions and 6 security question answers. I want to pass the data the user selects and enters from the view to the controller to save the data in session. ...
1
vote
1answer
315 views

LINQ to Entities query of tests for my classes, with many subqueries

This MVC4 controller method, which uses EF6, is taking a long time to run (over 2 seconds). I've added verbose comments for the purpose of this post only. ...
3
votes
1answer
283 views

Razor view to render simple scaffolding for complex type

After several attempts to follow my intuition that this code can be refactored to something more DRY, I keep hitting road blocks. Hoping someone can see a refactor that might get me going in a nicer ...
3
votes
4answers
186 views

Setting new lines and adding them to a list to be drawn

I have a method that basically passes in a known drawn line segment in my view and then draws parallel lines n distances away from the passed in original line. This one in particular draws two ...
6
votes
1answer
5k views

Unit of work, Ninject, Repository and WebAPI implementation

After recently getting a bulk of work done on my WebAPI service layer, I thought I'd post some of my working on here for you guys to tear apart, generally, most of it 'feels' mostly okay, but I know ...
4
votes
2answers
151 views

Return existing values inside database

Here's a method inside my controller that reads the values of angle and point data from my database. Then it grabs the data and adds it to a new list and sends the JSON to the view. I can't simplify ...
2
votes
2answers
5k views

Reading data from database, saving to new list and adding list to grid

I made some changes to how I was saving the data in order for it to show up on a kendo grid. Right now my giant method is getting data from the database (using ...
3
votes
2answers
374 views

MVC4 Unit Of Work with Entity FrameWork

I am designing a new asp.net MVC4 application. I know that context class generated by EF is Unit of work and Dbset is the repository class. I am just avoiding the extra abstraction. I want to know ...
18
votes
3answers
39k views

MVC Layered Project Structure

We are starting a new web project using C# / MVC4 and Entity Framework 5 for data access. I've decided to go with an n-layered approach for the structure of the project and I would like some feedback ...
3
votes
2answers
123 views

Removing nested for loops to hasten API call response time

I've got an MVC APU project that I'm working on to feed data to some JavaScript charts. This beast below takes nearly 1 second of server time to process before it is served to the chart. I know that ...
3
votes
1answer
1k views

Quiz form website

I'm trying to get a good grasp on ASP.Net MVC, Razor and RadioButtons, so I tried to make a quiz to test my skills. I implemented a working quiz form in the following way in Razor (not posting the ...
2
votes
1answer
599 views

Approach to MVC 4 windows group security to enable/disable controls in view

There are 3 window groups to add users and the application uses MVC 4. Here's my approach to add security attributes to my controllers and pass user's role to the view in order to enable/disable ...
3
votes
1answer
854 views

Navigating in ASP.NET MVC 4 in SPA-like App

I'm getting back into web development, and I'm creating a mobile-friendly web app. I'm trying to make it SPA-like in the sense that I don't load new pages for each action the user takes. As my index....