假设我将URL存储在数据库中,现在我希望表单操作属性或ActionLink URL指向存储在数据库中的URL。当我们使用ActionLink时,我们指定控制器和操作方法名称,与使用@ Html.BeginForm()时的方式相同,然后我们还指定控制器和操作方法名称。因此,我们如何自定义ActionLink和BeginForm()的代码,使其始终指向存储在数据库中的url。请用概念指导我。谢谢
对我来说,将html标签与模型放在一起太冗长,我会优先创建一个自定义的html helper,它将封装标签渲染背后的逻辑,您可以在此处查看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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句