Skip to main content

Questions tagged [asp.net-mvc-3]

ASP.NET MVC 3 is the third major version of Model-View-Controller extension for developing web applications in a .NET framework.

Filter by
Sorted by
Tagged with
897 votes
9 answers
456k views

Entity Framework 5 Updating a Record

I have been exploring different methods of editing/updating a record within Entity Framework 5 in an ASP.NET MVC3 environment, but so far none of them tick all of the boxes I need. I'll explain why. ...
Stokedout's user avatar
  • 11k
875 votes
23 answers
392k views

File Upload ASP.NET MVC 3.0

(Preface: this question is about ASP.NET MVC 3.0 which was released in 2011, it is not about ASP.NET Core 3.0 which was released in 2019) I want to upload file in asp.net mvc. How can I upload the ...
user637197's user avatar
  • 8,907
786 votes
12 answers
331k views

How do I import a namespace in Razor View Page?

How to import a namespace in Razor View Page?
Amitabh's user avatar
  • 60.4k
485 votes
7 answers
245k views

How to use ternary operator in razor (specifically on HTML attributes)?

With the WebForms view engine, I'll commonly use the ternary operator for very simple conditionals, especially within HTML attributes. For example: <a class="<%=User.Identity.IsAuthenticated ? "...
Portman's user avatar
  • 32k
461 votes
7 answers
309k views

Writing/outputting HTML strings unescaped

I've got safe/sanitized HTML saved in a DB table. How can I have this HTML content written out in a Razor view? It always escapes characters like < and ampersands to &amp;.
AGS's user avatar
  • 4,643
430 votes
7 answers
463k views

How to declare a local variable in Razor?

I am developing a web application in asp.net mvc 3. I am very new to it. In a view using razor, I'd like to declare some local variables and use it across the entire page. How can this be done? It ...
vondip's user avatar
  • 13.9k
417 votes
5 answers
251k views

Why is JsonRequestBehavior needed?

Why is Json Request Behavior needed? If I want to restrict the HttpGet requests to my action I can decorate the action with the [HttpPost] attribute Example: [HttpPost] public JsonResult Foo() { ...
gdoron's user avatar
  • 149k
384 votes
23 answers
251k views

Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine

I have this section defined in my _Layout.cshtml @RenderSection("Scripts", false) I can easily use it from a view: @section Scripts { @*Stuff comes here*@ } What I'm struggling with is how ...
tugberk's user avatar
  • 58.2k
373 votes
7 answers
1.2m views

How to set value of input text using jQuery

I have an input text which is this: <div class="editor-label"> @Html.LabelFor(model => model.EmployeeId, "Employee Number") </div> <div class="editor-field ...
raberana's user avatar
  • 11.8k
368 votes
17 answers
240k views

What's the difference between ViewData and ViewBag?

I saw the ViewBag in MVC 3. How's that different than ViewData in MVC 2?
user469652's user avatar
  • 50.5k
350 votes
6 answers
592k views

HTML.ActionLink vs Url.Action in ASP.NET Razor

Is there any difference between HTML.ActionLink vs Url.Action or they are just two ways of doing the same thing? When should I prefer one over the other?
Pankaj Upadhyay's user avatar
345 votes
25 answers
213k views

Multiple types were found that match the controller named 'Home'

I currently have two unrelated MVC3 projects hosted online. One works fine, the other doesn't work, giving me the error: Multiple types were found that match the controller named 'Home'. This can ...
Only Bolivian Here's user avatar
337 votes
3 answers
186k views

How do I specify different Layouts in the ASP.NET MVC 3 razor ViewStart file?

I would like to have 2 separate Layouts in my application. Let's say one is for the Public section of the website and the other is for the Member side. For simplicity, let's say all the logic for each ...
Justin's user avatar
  • 10.9k
326 votes
12 answers
491k views

Multiple models in a view

I want to have 2 models in one view. The page contains both LoginViewModel and RegisterViewModel. e.g. public class LoginViewModel { public string Email { get; set; } public string Password {...
Shawn Mclean's user avatar
  • 57.2k
309 votes
5 answers
71k views

ASP.NET MVC 3 - Partial vs Display Template vs Editor Template

So, the title should speak for itself. To create re-usable components in ASP.NET MVC, we have 3 options (could be others i haven't mentioned): Partial View: @Html.Partial(Model.Foo, "SomePartial") ...
RPM1984's user avatar
  • 72.8k
297 votes
9 answers
336k views

Non-static method requires a target

I have a controller action that works fine on Firefox both locally and in production, and IE locally, but not IE in production. Here is my controller action: public ActionResult MNPurchase() { ...
user547794's user avatar
  • 14.5k
286 votes
4 answers
57k views

ReSharper warns: "Static field in generic type"

public class EnumRouteConstraint<T> : IRouteConstraint where T : struct { private static readonly Lazy<HashSet<string>> _enumNames; // <-- static EnumRouteConstraint()...
bevacqua's user avatar
  • 48.2k
282 votes
8 answers
191k views

Replace line break characters with <br /> in ASP.NET MVC Razor view

I have a textarea control that accepts input. I am trying to later render that text to a view by simply using: @Model.CommentText This is properly encoding any values. However, I want to replace ...
bkaid's user avatar
  • 51.8k
267 votes
8 answers
351k views

Using Ajax.BeginForm with ASP.NET MVC 3 Razor

Is there a tutorial or code example of using Ajax.BeginForm within Asp.net MVC 3 where unobtrusive validation and Ajax exist? This is an elusive topic for MVC 3, and I cannot seem to get my form to ...
JBeckton's user avatar
  • 7,103
262 votes
1 answer
84k views

Html5 data-* with asp.net mvc TextboxFor html attributes

How do I add data-* html attributes using TextboxFor? This is what I currently have: @Html.TextBoxFor(model => model.Country.CountryName, new { data-url= Url.Action("CountryContains", "Geo") }) ...
Shawn Mclean's user avatar
  • 57.2k
261 votes
3 answers
132k views

@media media query and ASP.NET MVC razor syntax clash

I've got a large site that runs in ASP.NET MVC using the Razor view engine. I have a base stylesheet which contains all of the generic styling for the whole site. On occasion, however, I have page ...
Sniffer's user avatar
  • 6,312
259 votes
6 answers
168k views

An expression tree may not contain a call or invocation that uses optional arguments

An expression tree may not contain a call or invocation that uses optional arguments return this.RedirectToAction<MerchantController>(x => x.Edit(merchantId)); Where edit had a second, ...
bevacqua's user avatar
  • 48.2k
255 votes
8 answers
131k views

How do I define a method in Razor?

How do I define a method in Razor?
Rookian's user avatar
  • 20.3k
253 votes
6 answers
239k views

What is the @Html.DisplayFor syntax for?

I understand that in Razor, @Html does a bunch of neat things, like generate HTML for links, inputs, etc. But I don't get the DisplayFor function... Why would I write: @Html.DisplayFor(model => ...
Michael Pryor's user avatar
252 votes
6 answers
174k views

How to create a function in a cshtml template?

I need to create a function that is only necessary inside one cshtml file. You can think of my situation as ASP.NET page methods, which are min web services implemented in a page, because they're ...
Saeed Neamati's user avatar
246 votes
42 answers
323k views

"Parser Error Message: Could not load type" in Global.asax

I'm working on an MVC3 project and receive the following error: Parser Error Message: Could not load type 'GodsCreationTaxidermy.MvcApplication'. Source Error: Line 1: <%@ Application ...
PsychoCoder's user avatar
  • 10.7k
238 votes
2 answers
179k views

ASP.NET MVC 3 Razor: Include JavaScript file in the head tag

I'm trying to figure out the proper Razor syntax to get a JavaScript file for a particular *.cshtml to be in the head tag along with all the other include files that are defined in _Layout.cshtml.
Stephen Patten's user avatar
225 votes
23 answers
418k views

The type or namespace name does not exist in the namespace 'System.Web.Mvc'

Buiding MVC3 solution went well but have got an error in browser: Compiler Error Message: CS0234: The type or namespace name 'Html' does not exist in the namespace 'System.Web.Mvc' (are you missing ...
J.Olufsen's user avatar
  • 13.8k
223 votes
15 answers
196k views

Setting Access-Control-Allow-Origin in ASP.Net MVC - simplest possible method

I have a simple actionmethod, that returns some json. It runs on ajax.example.com. I need to access this from another site someothersite.com. If I try to call it, I get the expected...: Origin http:/...
Kjensen's user avatar
  • 12.2k
223 votes
6 answers
155k views

ViewBag, ViewData and TempData

Could any body explain, when to use TempData ViewBag ViewData I have a requirement, where I need to set a value in a controller one, that controller will redirect to Controller Two and Controller ...
Hari Gillala's user avatar
  • 11.9k
214 votes
3 answers
48k views

what is the function of webpages:Enabled in MVC 3 web.config

I just started a new MVC 3 project. Can anyone tell me what <add key="webpages:Enabled" value="false" /> in my web.config file does?
ftnilsson's user avatar
  • 2,193
212 votes
9 answers
209k views

Prevent Caching in ASP.NET MVC for specific actions using an attribute

I have an ASP.NET MVC 3 application. This application requests records through jQuery. jQuery calls back to a controller action that returns results in JSON format. I have not been able to prove this, ...
JavaScript Developer's user avatar
212 votes
4 answers
133k views

ASP.NET MVC3 - textarea with @Html.EditorFor

I have ASP.NET MVC3 app and I have also form for add news. When VS2010 created default view I have only text inputs for string data, but I want to have textarea for news text. How I can do it with ...
Jacob Jedryszek's user avatar
210 votes
7 answers
138k views

Where and how is the _ViewStart.cshtml layout file linked?

Here's the About.cshtml from the default MVC 3 template: @{ ViewBag.Title = "About Us"; } <h2>About</h2> <p> Put content here. </p> I would expect that a reference ...
Kman's user avatar
  • 4,991
208 votes
16 answers
241k views

include antiforgerytoken in ajax post ASP.NET MVC

I am having trouble with the AntiForgeryToken with ajax. I'm using ASP.NET MVC 3. I tried the solution in jQuery Ajax calls and the Html.AntiForgeryToken(). Using that solution, the token is now being ...
OJ Raqueño's user avatar
  • 4,521
208 votes
2 answers
211k views

How to write a comment in a Razor view?

How to write a comment in a MVC view, that won't be transmitted to the final HTML (i.e.,to browser, to response). One can make a comment with: <!--<a href="/">My comment</a> --> ...
horgh's user avatar
  • 18.4k
206 votes
4 answers
149k views

Access key value from Web.config in Razor View-MVC3 ASP.NET

How do I access a key value from web.config in my Razor view. This is in my web.config in the Web Project root level. <appSettings> <add key="myKey" value="MyValue"/> </...
Hari Gillala's user avatar
  • 11.9k
202 votes
16 answers
228k views

ASP.NET MVC 3 Razor - Adding class to EditorFor

I'm trying to add a class to an input. This is not working: @Html.EditorFor(x => x.Created, new { @class = "date" })
user137348's user avatar
  • 10.3k
202 votes
4 answers
153k views

Generic method multiple (OR) type constraint

Reading this, I learned it was possible to allow a method to accept parameters of multiple types by making it a generic method. In the example, the following code is used with a type constraint to ...
Mansfield's user avatar
  • 14.9k
199 votes
7 answers
258k views

EF LINQ include multiple and nested entities

Ok, I have tri-leveled entities with the following hierarchy: Course -> Module -> Chapter Here was the original EF LINQ statement: Course course = db.Courses .Include(i => i....
AnimaSola's user avatar
  • 7,686
188 votes
5 answers
153k views

Differences between Html.TextboxFor and Html.EditorFor in MVC and Razor

Why by default were these changed when adding a new "edit" view? What are advantages when using EditorFor() vs. TextboxFor()? I found this By default, the Create and Edit scaffolds now use ...
ShaneKm's user avatar
  • 21.2k
187 votes
10 answers
255k views

Add CSS or JavaScript files to layout head from views or partial views

Layout pages head: <head> <link href="@Url.Content("~/Content/themes/base/Site.css")" rel="stylesheet" type="text/css" /> </head> A View (AnotherView) from the ...
Nuri YILMAZ's user avatar
  • 4,301
185 votes
3 answers
351k views

How to set web.config file to show full error message

I deployed my MVC-3 application on windows Azure. But now when I am requesting it through staging url it shows me (Sorry, an error occurred while processing your request.). Now I want to see the full ...
King Kong's user avatar
  • 2,905
184 votes
9 answers
170k views

StringLength vs MaxLength attributes ASP.NET MVC with Entity Framework EF Code First

What is the difference in behavior of [MaxLength] and [StringLength] attributes? As far as I can tell (with the exception that [MaxLength] can validate the maximum length of an array) these are ...
Nick Goloborodko's user avatar
180 votes
4 answers
131k views

How to use ? : if statements with Razor and inline code blocks

I'm updating my old .aspx views with the new Razore view engine. I have a bunch of places where I have code like this: <span class="vote-up<%= puzzle.UserVote == VoteType.Up ? "-selected" : "" %...
Micah's user avatar
  • 114k
168 votes
8 answers
125k views

Razor HtmlHelper Extensions (or other namespaces for views) Not Found

I don't know if this was happening in the PR or Beta, but if I create an extension method on HtmlHelper, it is not recognized in a Razor powered page: namespace SomeNamespace.Extensions { public ...
swilliams's user avatar
  • 48.6k
167 votes
6 answers
105k views

Do asynchronous operations in ASP.NET MVC use a thread from ThreadPool on .NET 4

After this question, it makes me comfortable when using async operations in ASP.NET MVC. So, I wrote two blog posts on that: My Take on Task-based Asynchronous Programming in C# 5.0 and ASP....
tugberk's user avatar
  • 58.2k
166 votes
29 answers
329k views

The type or namespace name 'DbContext' could not be found [closed]

I am VERY new to ASP.NET MVC (3) and am having a hard time resolving a build error in Visual Studio: The type or namespace name 'DbContext' could not be found (are you missing a using directive or an ...
Chris's user avatar
  • 4,528
163 votes
7 answers
306k views

RestSharp JSON Parameter Posting

I am trying to make a very basic REST call to my MVC 3 API and the parameters I pass in are not binding to the action method. Client var request = new RestRequest(Method.POST); request.Resource = "...
Wesley Tansey's user avatar
163 votes
8 answers
398k views

Redirect to Action in another controller

I have two controllers, both called AccountController. One of them, lets call it Controller A, is in an Area called Admin and the other, lets call it Controller B, is not in any Area (I guess that ...
Sachin Kainth's user avatar

1
2 3 4 5
767