asp.net-mvc – 使用Html.BeginForm与querystring

我的网址如下所示:
customer/login?ReturnUrl=home

登录视图中,我使用了这种可以正常工作的代码模式。

@using(Html.BeginForm())
{
   ...
}

这神奇地生成以下html

<form action="customer/login?ReturnUrl=home" method="post">

但现在,我需要在表单中添加一个属性(例如,data-id =“something”)。我怎样才能做到这一点?如果我没有任何查询字符串,我知道我可以这样做:

@using(Html.BeginForm(action,controller,FormMethod.Post,new { data_id="something" }))

但是不知道如何添加应该在html中的querystring:

<form action="customer/login?ReturnUrl=home" method="post" data-id="something">

我想到了使用< form>直接但不知道如何指定querystring哪个是可变的。我不知道如何用Html.BeginForm来实现它。任何提示将不胜感激。

解析度:

现在,我使用了< form>具有以下提示How to get current url value in View.结果视图看起来像

<form action="@Request.Url.PathAndQuery" data-id="something" method="POST">

但是对于这一点,可以使用BeginForm的重载方法

解决方法

我猜这不直接回答这个问题,但是为什么不使用一个简单的旧格式标签
<form action='customer/login?ReturnUrl=@Request.QueryString["ReturnUrl"]' method="post" data-id="something">

或者,您可以创建一个自定义的HtmlHelperExtension,它使用路径和查询字符串呈现表单。在这个HtmlHelperExtension中,您可以遍历查询字符串值并填充routeValueDictionary,然后传递给Html.BeginForm构造函数

如果你不想要这样可扩展的东西,你可以使用Html.BeginForm的重载构造函数@ Html.BeginForm(“login”,“customer”,new {ReturnUrl = @ Request.QueryString [“ReturnUrl”]},FormMethod.Post,new {data-id =“something”});

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...