English 中文(简体)
提交行动链接
原标题:Rendering an ActionLink programmatically

我有这样一种设想,即我愿意从方案上,从控制器中提取“行动链接”,或者理想的是一种延伸方法。 因此,我可以总结一下我们的一些逻辑,以显示我们的联系,并从我的角度来看消除这一逻辑。

我可以使用《刑法》中的MVC未来项目创建行动链接。

ActionLink actionLink = new ActionLink();
actionLink.ActionName = "MyActionName";
actionLink.ControllerName = "MyControllerName";

有一种采用HtmlTextWriter的RenderControl方法,我曾试图使用这种方法。 我无法做到这一点,在微软公司内部每次都出现一个无效的例外。 Web.Mvc Assembly (Futures).

[NullReferenceException: Object reference not set to an instance of an object.]
   Microsoft.Web.Mvc.Controls.ActionLink.Render(HtmlTextWriter writer) +643
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   MvcSpike.Controllers.HomeController.Index() in C:...ControllersHomeController.cs:33
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +74
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a() +52
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254
   System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +314
   System.Web.Mvc.Controller.ExecuteCore() +105
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +39
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +34
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +59
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +44
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8679150
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

任何帮助都是巨大的。

最佳回答

假设你重新担任控制员,你可以采取以下措施:

string anchor = HtmlHelper.GenerateLink(
                    this.Request.RequestContext,
                    RouteTable.Routes,
                    "Link name",
                    "",
                    "MyActionName",
                    "MyControllerName",
                    null,
                    null);

但我并不完全相信你最终会看到什么。

问题回答

最好的方式是将控制器设定的国旗储存在模型中,不论是否显示这种联系,并以此方式展示/展示。 否则,你会想以这种逻辑来建立自己的帮助方法;它基本上使tag子成为主角,并利用乌尔尔赫尔珀类行动方法产生ur。 您也可以在控制器中使用乌尔埃尔克语,并通过模型,或者在GisData等中使用。

HTH.





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签