Asp.net mvc 3 如何从数据库ASP.NETMVC生成和呈现url
假设我将url存储在数据库中,现在我希望我的表单操作属性或ActionLink url应该指向存储在数据库中的url。当我们使用ActionLink时,我们指定控制器和操作方法名称,当我们使用@Html.BeginForm时,我们也指定控制器和操作方法名称。所以,我们如何定制ActionLink&BeginForm的代码呢?因此,它应该始终指向存储在数据库中的url。请用概念指导我。谢谢如果要使用存储在数据库中的url,为什么要使用ActionLink或BeginForm helpersAsp.net mvc 3 如何从数据库ASP.NETMVC生成和呈现url,asp.net-mvc-3,Asp.net Mvc 3,假设我将url存储在数据库中,现在我希望我的表单操作属性或ActionLink url应该指向存储在数据库中的url。当我们使用ActionLink时,我们指定控制器和操作方法名称,当我们使用@Html.BeginForm时,我们也指定控制器和操作方法名称。所以,我们如何定制ActionLink&BeginForm的代码呢?因此,它应该始终指向存储在数据库中的url。请用概念指导我。谢谢如果要使用存储在数据库中的url,为什么要使用ActionLink或BeginForm helpers <
<a href="@Model.UrlComingFromYourDatabase">Click me</a>
看起来很好。这些帮助程序设计用于通过提供控制器和操作名称来组成url。对我来说,将html标记与模型放在一起太冗长了,我希望创建一个自定义html帮助程序,它将封装标记呈现背后的逻辑。您可以查看mvc代码,但可能是这样的:
private static MvcForm MyFormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes)
{
//you can use service locator for getting your database artifacts
//place your custom logic
TagBuilder tagBuilder = new TagBuilder("form");
tagBuilder.MergeAttributes(htmlAttributes);
// action is implicitly generated, so htmlAttributes take precedence.
tagBuilder.MergeAttribute("action", formAction);
// method is an explicit parameter, so it takes precedence over the htmlAttributes.
tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true);
bool traditionalJavascriptEnabled = htmlHelper.ViewContext.ClientValidationEnabled
&& !htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled;
if (traditionalJavascriptEnabled)
{
// forms must have an ID for client validation
tagBuilder.GenerateId(htmlHelper.ViewContext.FormIdGenerator());
}
htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag));
MvcForm theForm = new MvcForm(htmlHelper.ViewContext);
if (traditionalJavascriptEnabled)
{
htmlHelper.ViewContext.FormContext.FormId = tagBuilder.Attributes["id"];
}
return theForm;
}
为什么不在href属性中的锚定标记中显示值呢?在这种情况下,如何指定url?假设url存储在变量中,并且该变量需要用于设置表单操作…如何使用BeginForm?任何指引!您可以这样做:……ViewContext.FormIdGenerator是一个内部方法,您不能仅在代码中这样做。