How can I return the current action in an ASP.NET MVC view?
Use the ViewContext
and look at the RouteData
collection to extract both the controller and action elements. But I think setting some data variable that indicates the application context (e.g., "editmode" or "error") rather than controller/action reduces the coupling between your views and controllers.
Get current action in mvc view model
You can add a property in your viewmodel that contains what it is used for. You can set this in the controller.
Get attribute of current controller action in MVC View
You should first make a reflection to the method with Type.GetMethodInfo
string actionName = ViewContext.RouteData.Values["Action"]
MethodInfo method = type.GetMethod(actionName);
var attribute = method.GetCustomAttributes(typeof(DisplayNameAttribute), false);
if (attribute.Length > 0)
actionName = ((DisplayNameAttribute)attribute[0]).DisplayName;
else
actionName = type.Name; // fallback to the type name of the controller
And then you can pass the actionName to the View using something like
ViewBag.name = actionName;
and then get the Viewbag variable from the view
Return view then action in MVC
You need to get the client to redirect the browser on page load, to automate it add this at the end of the view before the </html>
tag:
<script type="text/javascript">
window.location = "@Url.Action("ActionName", "ControllerName")";
</script>
Although I suspect there better solutions if we know the specific problem you are trying to overcome.
ASP.NET MVC Return Model From View To Action Method
You don't need the entire object here:
public ActionResult Delete(tAdminAlerts obj)
You just need an identifier for the record being deleted, which you'd then use to delete that record in the data:
public ActionResult Delete(int id)
You can then just create a simple link to that action. Different versions of ASP.NET MVC may have slight differences, but in general you could create an Action Link with the link text, action name, (optional) controller name, and route values. For example:
@Html.ActionLink("Delete", "Delete", new { id = obj.id })
(I'm assuming the identifier on obj
in the view is called id
, use whatever identifier you want to use.)
This would just create a simple link for the user to click on to take them to that Delete
action.
ASP.NET MVC - Current Action from controller code?
You can access the route data from within your controller class like this:
var actionName = ControllerContext.RouteData.GetRequiredString("action");
Or, if "action" isn't a required part of your route, you can just index into the route data as per usual.
Returning to current view after actions in _Layout.cshtml
I found a completely different (and much easier and elegant) solution to my problem.
I simply created a BaseController
, that holds the action for changing the localization.
Then all controllers I add to the site inherit from this BaseController
. This gives a single location for the code and does not require sending any return parameters, etc.
BaseController
:
public class BaseController : Controller
{
[HttpPost]
public ActionResult Index(string localization)
{
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(localization);
return View();
}
}
Each of the site's Controllers
then only need to inherit from it, and then mind their own actions:
public class ApplicationsController : BaseController
{
//
// GET: /Applications/
public ActionResult Index()
{
return View();
}
}
...
Related Topics
Send JSON via Post in C# and Receive the JSON Returned
Login Using Google Oauth 2.0 with C#
System.Badimageformatexception: Could Not Load File or Assembly
What's the Role of Gethashcode in the Iequalitycomparer<T> in .Net
Differencebetween Streamwriter.Flush() and Streamwriter.Close()
Customattribute Reflects HTML Attribute MVC5
How to Know If a Cell Has an Error in the Formula in C#
ASP.NET Core Content-Disposition Attachment/Inline
Httpwebrequest & Native Gzip Compression
Regular Expression to Allow Backslash in C#
Dbset.Attach(Entity) VS Dbcontext.Entry(Entity).State = Entitystate.Modified
Find() VS. Where().Firstordefault()
Datetime2' Error When Using Entity Framework in VS 2010 .Net 4.0
How to Display a Windows Form in Full Screen on Top of the Taskbar