Html.ActionLink as a button or an image, not a link

I like to use Url.Action() and Url.Content() like this: <a href=”https://stackoverflow.com/questions/596444/@Url.Action(“MyAction”, “MyController”)”> <img src=”@Url.Content(“~/Content/Images/MyLinkImage.png”)” /> </a> Strictly speaking, the Url.Content is only needed for pathing is not really part of the answer to your question. Thanks to @BrianLegg for pointing out that this should use the new Razor view syntax. Example has been updated accordingly.

How to use dashes in HTML-5 data-* attributes in ASP.NET MVC

This problem has been addressed in ASP.Net MVC 3. They now automatically convert underscores in html attribute properties to dashes. They got lucky on this one, as underscores are not legal in html attributes, so MVC can confidently imply that you’d like a dash when you use an underscore. For example: @Html.TextBoxFor(vm => vm.City, new … Read more

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

Yes, there is a difference. Html.ActionLink generates an <a href=”https://stackoverflow.com/questions/7709001/..”></a> tag whereas Url.Action returns only an url. For example: @Html.ActionLink(“link text”, “someaction”, “somecontroller”, new { id = “123” }, null) generates: <a href=”https://stackoverflow.com/somecontroller/someaction/123″>link text</a> and Url.Action(“someaction”, “somecontroller”, new { id = “123” }) generates: /somecontroller/someaction/123 There is also Html.Action which executes a child controller action.

ASP.NET MVC View Engine Comparison

ASP.NET MVC View Engines (Community Wiki) Since a comprehensive list does not appear to exist, let’s start one here on SO. This can be of great value to the ASP.NET MVC community if people add their experience (esp. anyone who contributed to one of these). Anything implementing IViewEngine (e.g. VirtualPathProviderViewEngine) is fair game here. Just … Read more

How to add Web API to an existing ASP.NET MVC 4 Web Application project?

The steps I needed to perform were: Add reference to System.Web.Http.WebHost. Add App_Start\WebApiConfig.cs (see code snippet below). Import namespace System.Web.Http in Global.asax.cs. Call WebApiConfig.Register(GlobalConfiguration.Configuration) in MvcApplication.Application_Start() (in file Global.asax.cs), before registering the default Web Application route as that would otherwise take precedence. Add a controller deriving from System.Web.Http.ApiController. I could then learn enough from the … Read more

Difference between ApiController and Controller in ASP.NET MVC

Use Controller to render your normal views. ApiController action only return data that is serialized and sent to the client. here is the link Quote: Note If you have worked with ASP.NET MVC, then you are already familiar with controllers. They work similarly in Web API, but controllers in Web API derive from the ApiController … Read more

How can I properly handle 404 in ASP.NET MVC?

The code is taken from http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx and works in ASP.net MVC 1.0 as well Here’s how I handle http exceptions: protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // Log the exception. ILogger logger = Container.Resolve<ILogger>(); logger.Error(exception); Response.Clear(); HttpException httpException = exception as HttpException; RouteData routeData = new RouteData(); routeData.Values.Add(“controller”, “Error”); if … Read more

What is ViewModel in MVC?

A view model represents the data that you want to display on your view/page, whether it be used for static text or for input values (like textboxes and dropdown lists) that can be added to the database (or edited). It is something different than your domain model. It is a model for the view. Let … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)