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.
asp.net-mvc-3
38,348
questions
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.
...
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 ...
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?
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 ? "...
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 &.
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 ...
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()
{
...
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 ...
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 ...
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?
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?
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 ...
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 ...
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 {...
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")
...
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()
{
...
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()...
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 ...
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 ...
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") })
...
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 ...
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, ...
255
votes
8
answers
131k
views
How do I define a method in Razor?
How do I define a method in Razor?
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 => ...
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 ...
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 ...
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.
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 ...
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:/...
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 ...
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?
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, ...
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 ...
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 ...
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 ...
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> -->
...
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"/>
</...
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" })
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 ...
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....
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 ...
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 ...
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 ...
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 ...
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" : "" %...
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 ...
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....
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 ...
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 = "...
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 ...