How to find view name for action method - asp.net-mvc

For instance this action
public class UserController : Controller
{
public ActionResult SomeAction()
{
// some code here
return View();
}
}
How do we know the view name for the SomeAction()?

Related

Controller level parameter with ASP.NET MVC attribute routing

Is there any way to add a parameter to the Controller Routing attribute?
something like:
[Route("controller/{id}/"]
public class Controller {
public Controller(string id) { /*..*/ }
[HttpGet]
public ActionResult Get() { /*..*/ }
}
The [FromRoute] attribute on your controller method arguments should get you what you need.
Example
[Route("api/Test/{testId}")]
public class TestController: ControllerBase
{
[HttpGet]
[Route("echo")]
public void TestMethod([FromRoute]string testId)
{
return testId;
}
}
Also see this S.O. for more information.

Umbraco custom viewmodel

I would like to pass the data to the view, I created a viewmodel inheriting from RenderModel but when I run to the error "Element" Umbraco.Web.Models.RenderModel "does not contain the definition" Topic "
ViewModel:
namespace Umbraco12.Models
{
public class Home : RenderModel
{
public Home(IPublishedContent content, CultureInfo culture) : base(content, culture)
{
}
public string Topic { get; set; }
}
}
Controller:
public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
// GET: Home
public ActionResult Home(RenderModel model)
{
var home = new Home(model.Content, model.CurrentCulture);
home.Topic = "aloha";
//Do some stuff here, then return the base method
return View("Home", home);
}
}
View:
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage<Home>
#using ContentModels = Umbraco.Web.PublishedContentModels;
#{
Layout = "Master.cshtml";
}
<h1>#Umbraco.Field("topic") : #Model.Topic</h1>
Make sure your DocumentType Alias is "Home".
I would also call your model something different to just Home as it could clash and give an ambiguous error.
Controller:
public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
// GET: Home
public ActionResult Index(RenderModel model)
{
var home = new Home(model.Content, model.CurrentCulture);
home.Topic = "aloha";
//Do some stuff here, then return the base method
return View("Home", home);
}
}
View:
#inherits UmbracoViewPage<Umbraco12.Models.Home>
#{
Layout = "Master.cshtml";
}
<h1>#Model.Topic</h1>

MVC URL Rewriting redirect with original name

I have a controller named HomeController, I am re-writing its all actions and even the name of controller,by decorating them with Route and Routeprefix respectively.
This is my controller with action method Index
[RoutePrefix("MyPortal")]
public class HomeController : Controller
{
[Route("Home")]
public ActionResult Index()
{
}
[Route("Index")]
public ActionResult LandingPage()
{
}
}
Its working fine,but when I type Home/Index in URL,its giving me error
A public action method 'index' was not found on controller 'Web.Controllers.HomeController'. /Home/index
I want to redirect the user to Index ActionResult,if he type Home/Index or MyPortal/Home
Similarly, he should redirect to LandingPage, if he type Home/LandingPage or MyPortal/Index.
MyPortal/Home or MyPortal/Index is working fine.
How about this one ..? But your last case (MyPortal/Index) will not work
[RoutePrefix("Home")]
[RoutePrefix("MyPortal")]
public class HomeController : Controller
{
[Route("Index")]
[Route("Home")]
public ActionResult Index()
{
}
[Route("LandingPage")]
public ActionResult LandingPage()
{
}
}

Asp.Net MVC call method when every view is called

I have some ViewBag need to pass to every views when them are called.
For example:
public ActionResult Index()
{
ViewBag.Importan1 = "A";
ViewBag.Importan2 = "B";
return View();
}
public ActionResult Detail()
{
ViewBag.Importan1 = "A";
ViewBag.Importan2 = "B";
return View();
}
and I want to:
public ActionResult Index()
{
return View();
}
public ActionResult Detail()
{
return View();
}
but the ViewBag Important1 and Important2 are called implicit.
I would suggest you a custom filter attribute. You create it (here some good examples http://msdn.microsoft.com/en-us/library/dd381609(v=vs.100).aspx) and do all the work inside. Then you just have to apply the filter on your controller and it will affect all your action methods.
You can write a super class for the controller defines Index and Detail as virtual (to be able to override them and add some extra functionality) and set Important1 and Important2 in your base class as
public class MyController : Controller
{
public virtual ActionResult Index()
{
ViewBag.Importan1 = "A";
ViewBag.Importan2 = "B";
return View();
}
public virtual ActionResult Detail()
{
ViewBag.Importan1 = "A";
ViewBag.Importan2 = "B";
return View();
}
}
that is it, you have Index and Detail with your desired behavior in all of your controllers which is inherited from MyController from example
public class Controller1 : MyController
{
public override ActionResult Index()
{
var result = base.Index();
//do some manipulation here
return result;
}
}
Another Solution
You can write this controller (I didn't check that on my pc)
public class MyController : Controller
{
protected override void OnActionExecuting(
ActionExecutingContext filterContext)
{
//write your custom code here
}
}
then inherit your controller from MyController
Another Solution
using filterActionAttribute and write your own filer action attribute

Redirect to same view

I am working on a ASP.NET MVC website and I am new to this.
I have a controller with few actions. I want to use these actions through out my website.
For example
[HttpPost]
public ActionResult MyAction(ViewModel model)
{
if (ModelState.IsValid)
{
//code is here
}
return RedirectToAction(); // redirect to same view
}
I want to redirect to same view from where request is generated. I am not sure if this is possible or not ?
Based on your comment, I would create a Controller that looks like:
public MyController : Controller
{
private ActionResult SharedMethod(SomeModel model)
{
if (ModelState.IsValid)
{
//code is here
}
// viewname is required, otherwise the view name used will be
// the original calling method (ie public1.cshtml, public2.cshtml)
return this.View("SharedViewName");
}
public ActionResult Public1(SomeModel model)
{
return this.SharedMethod(model);
}
public ActionResult Public1(SomeModel model)
{
return this.SharedMethod(model);
}
}

Resources