Add the name space in your controller at the top
using Microsoft.AspNet.Identity;
Wherever you need to get the user id use the following code in your controller
string userId = User.Identity.GetUserId();
Add the name space in your controller at the top
using Microsoft.AspNet.Identity;
Wherever you need to get the user id use the following code in your controller
string userId = User.Identity.GetUserId();
The easiest way to render an HTML link in is to use the HTML.ActionLink() helper.
With MVC, the Html.ActionLink() does not link to a view. It creates a link to a controller action.
The first parameter is the link text, and the second parameter is the name of the controller action.
The Html.ActionLink() helper above, outputs the following HTML:
When using @html.actionlink it normally renders it like a hyperlink. But there is a simple way to render it as a button.
@Html.ActionLink("Register,"Register")
and it will render it as a Hyperlink
I am using Boostrap so I have the CSS class called “btn” available. Otherwise you can use your own CSS which can create a button
@Html.ActionLink("Register", "Register" ,null, new { @class = "btn" })
@Html.ActionLink("Register", "Register" ,null, new with {.class = "btn" })
the null is for the Routevalues ( in the example we are using any route values hence null)
Hope this helps you
If you want to password protect some pages on your MVC4 site using the Forms Authentication then it is best to to protect the whole site and then selectively allow Anonymous Access to the pages which are to be shown to every visitor.
Open the FilterConfig.VB file and add the line
filters.Add(New AuthorizeAttribute) in it
Once you run your application you would see that all the pages of your website are now protected, the users have to register before they can access.
Imports System.Web Imports System.Web.Mvc
Public Class FilterConfig
Public Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
filters.Add(New HandleErrorAttribute())
filters.Add(New AuthorizeAttribute)
End Sub
End Class
If you need to allow some pages for anonymous view then add the following decoration.
<AllowAnonymouse()> on each page which you want to open up for anonymous access.
<AllowAnonymous()>
Function Index() As ActionResult
ViewData("Message") = "Modify this template to jump-start your ASP.NET MVC application."
Dim rep As New RepositoryRealEstate
ViewBag.AgentCount = rep.GetAgentsCount
ViewBag.ProperyCount = rep.GetPropertyCount
ViewBag.VendorCount = rep.getvendorcount
Return View()
End Function
In your Create Actions in a Controller in MVC4, if you try to save text with html tags then you get this annoying error.
A potentially dangerous Request.Form value was detected from the client
Description: ASP.NET has detected data in the request that is potentially dangerous because it might include HTML markup or script. The data might represent an attempt to compromise the security of your application, such as a cross-site scripting attack. If this type of input is appropriate in your application, you can include code in a web page to explicitly allow it. For more information, see http://go.microsoft.com/fwlink/?LinkID=212874.
This is a great protection if you do not want your client to save html text.
Here is how to get rid of this error
In the following example we have a create action which saves a new record. MVC4 controller will have two functions.
The first function is for GET ( this is the one which will render the page to save the new record
The second function is for POST. This function is responsible to receive the model
which has been populated by the data which the user has filled in on the form. We have to add the following text
<ValidateInput(False)>
for C# add
[ValidateInput(false)]
‘ GET: /admin/ManageAds/Create
Function Create() As ActionResult
Return View()
End Function
‘ POST: /admin/ManageAds/Create
<HttpPost()> _
<ValidateAntiForgeryToken()> _
<ValidateInput(False)>
Function Create(ByVal ad As Ad) As ActionResult
If ModelState.IsValid Then
db.Ads.Add(ad)
db.SaveChanges()
Return RedirectToAction(“Index”)
End If
Return View(ad)
End Function
If you are using the Twitter Bootsrap in you MVC4 project and you have a need to display a boostrap button then here is a simple way to use it with the @html.actionlink
@Html.ActionLink(“download”, “download”, New With {.rid = Model.RingID}, New With {.class = Html.Raw(“btn btn-primary btn-mini”)})
How to use Paging using the PagedList
Once you have installed the PagedList package from NUGET
Controller
Add the following two imports at the top of the header
Imports PagedList
Imports PagedList.Mvc
Your Linq or SQL query should be sorted
eg.
Dim v = (From vid In db.qry_Videos Where vid.CatID = CatID Order By vid.OrderByID Select vid)
ViewBag.CurrentSort = v ‘ This is most important you need to tell the view what your current sort order is
Return View(v)
View
In your view add the following imports at the top of the view
@imports PagedList
@imports PagedList.Mvc
Add the style sheet in your view as well. This will help you render your pager properly
<link href=”~/Content/PagedList.css” rel=”stylesheet” />
Wherever you want your pager to appear use the following
“index” is the name of your view, if your view is named different then change it to your view
“page” – this is the variable which stores the current page number
“ViewBag.search – this is the ViewBag which you used in your controller
OnlyShowFivePagesAtATime – this tells the pager how many buttons will be shown at one time ( this option shows
@Html.PagedListPager(DirectCast(ViewBag.CurrentSort, IPagedList), Function(page) Url.Action(“index“, New With {.page = page, .criteria = ViewBag.search}), PagedListRenderOptions.OnlyShowFivePagesAtATime)
I realised that in my MVC5 project in Visual Studio I could not get the Ajax.BeginForm to work properly. It will always do a full postback.
I was trying to complete a Video Rating Page, where there are buttons to rate a video and once the user presses one of the rating button, it should thank the user and display the new rating value. I wanted this to shown on the same page without reloading the page, So I considered using AJAX.BeginForm which will simply work fine.
In order to have this AJAX functionality you will have to download the AJAX from NUGET.
Step 1
Install the following two highlighted packages from NUGET. Click here to check how to load Packages from NUGET in Visual Studio
Step 2
Once you have installed the above two packages through NUGET then do the following.
Go to the _Layout file in your _shared folder in the Views. ( Note that I am using VB.NET but the example will work for C# also.
Open the _Layout.vbhtml file and browse to the bottom.
You will see the following two lines.
@Scripts.Render(“~/bundles/jquery”)
@Scripts.Render(“~/bundles/bootstrap”)
Now add the following line after the above two lines.
<script src=”~/Scripts/jquery.unobtrusive-ajax.min.js”></script>
I made a mistake of adding this script at the top of my page and my AJAX calls refused to work.
Then I realised that the AJAX has to be loaded after loading the jquery. So I added it at the last.
Step 3
Run your application.
Your AJAX Calls should work fine.