In MVC2 I have used Page.User.Identity.Name using the <%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
How can I use the same in MVC3?
You can always do something like:
#Html.ViewContext.HttpContext.User.Identity.Name
but don't.
Normally a view shouldn't try to fetch such information. It is there to display whatever information is passed by the controller. It should be strongly typed to a model class which is passed by a controller action.
So in the controller action rendering this view:
[Authorize]
public ActionResult Index()
{
var model = new MyViewModel
{
Username = User.Identity.Name
}
return View(model);
}
Now inside the view feel free to use this information:
#Model.Username
MVC 2
<%: this.Page.User.Identity.Name %>
MVC 3
#this.User.Identity.Name
I had the same problem. I used this tutorial to solve this issue.
In short, in your view, put this code:
#Context.User.Identity.Name
Just make sure that the project is set to authenticate with windows.
Related
Hi,
I have a ASP.NET MVC 3 website where I have created a action that returns a partial view. This view have the following :
<%# OutputCache Duration="10000" VaryByParam="none" %>
And to render this from my main view I use this :
<% Html.RenderAction("ImageWall", "Image"); %>
The problem is that it is not caching at all? When setting a breakpoint I can see that the expensive drawing is running everytime?
For this, Use Action Attribute
<OutputCache(Duration:=10000, VaryByParam:="any")>
Function ImageWall() As PartialViewResult
Return PartialView()
End Function
For Vb.Net sample, sorry
I have the following header of ASP.Net MVC page:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<NEOGOV_Ideas.Models.SubIdeaAdminPage>"
ValidateRequest="false" %>
I need to move this page to Razor syntax. How should I set ValidateRequest?
Thanks
Decorate your action method with ValidateInput attribute
[HttpPost]
[ValidateInput(false)]
public ActionResult index()
{
return view();
}
You shouldn't need that line in the view, instead use the ValidateInput(false) attribute on the controller method.
Make sure you've got this in your web.config if you're using ASP .net 4.0 (which I presume you are if you're using MVC 3)
<httpRuntime requestValidationMode="2.0"/>
Martin
From MVC 4 we can allow html content only for property of model class, not for the whole request. Just need to mark property by attribute AllowHtml
public class EditorialPixlocateRequestViewModel
{
[AllowHtml]
public string Xml { get; set; }
}
In my asp.net mvc3 application i have created two partial views for two different action that is,
partialviewresult setcomment and
partialviewresult getcomment
i have created partial view using create a strongly type view and different scaffold template
for _setcomment i am using create template and for _getcomment i am using List template.
Now i want to call both _setcomment and _getcomment partial view in one view.
in my view file .cshtml
_setcomment -
#model <NAMESPACE>.<MODELNAME>
<some code>
_getcomment -
#model IEnumerable<<NAMESPACE>.<MODELNAME>>
<some code>
how can i call diiferent partial view in one view?
any suggestions?
There are different ways to do it.
If you already have the model class data in the Main view you can use like
In the main view call
#Html.Partial("PartialViewName1",model1)
#Html.Partial("PartialViewName1",model2)
If you do not have the model class data in the mail view then you can call the action on the controller and from there return the partial view.
#Html.Action("Controller","Action1")
#Html.Action("Controller","Action2")
In the Controller class
PartialResult Action1()
{
model = new ModelClass();
return PartialView(model);
}
Hope this helps.
The answer to your question is to use the following within a single view:
#{ Html.RenderAction("ActionName", "ControlerName"); }
#{ Html.RenderAction("ActionName2", "ControlerName2"); }
This would do what you are trying to achieve, however, I think there is a problem with design. What are you trying to achieve?
The strongly-typed action-link helpers in MvcContrib do not appear to work with areas:-
namespace MySite.Areas.Bar.Controllers
{
public class FooController
...
and
<%: Html.ActionLink<FooController> %>
routes to /Foo, rather than /Bar/Foo
What other solutions are available for creating strongly-typed actionlinks that work with areas?
You could decorate your controller with the [ActionLinkArea] attribute to indicate that this controller is part of an area (yes there is no way for knowing this at runtime other than using reflection and looking if the containing namespace has Area in it which is not reliable. This behavior will change in ASP.NET MVC 4 where areas will be first class citizens of the framework):
[ActionLinkArea("Bar")]
public class FooController : Controller
{
public ActionResult Index()
{
return View();
}
}
Now the following will generate proper link:
<%= Html.ActionLink<FooController>(x => x.Index(), "Go to index in Bar area") %>
In ASP.NET WebForms, I can use the CodeBehind of a master page to fetch data to use to bind up my navigation.
How can I achieve the same in ASP.NET MVC 3?
Ideally the main navigation would be in the _Layout.cshtml but this file doesn't have it's own model. i.e. It can only use the model supplied by the controller action (assuming a base class and #model directive in the _Layout.cshtml.
Edit
While I realise MVC does not have the notion of DataBinding, I included it here to help describe the functionality I'm looking for.
How can I achieve the same in ASP.NET MVC 3?
The notion of databinding is not common for the MVC pattern. To implement the navigation you could use Html.Action and Html.RenderAction.
Example:
public class NavigationController : Controller
{
public ActionResult Index()
{
NavigationViewModel model = ...
return View(model);
}
}
and then inside the layout:
#Html.Action("Index", "Navigation")
The index.cshtml could be a partial which implements the navigation.