加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 00:41:05 所属栏目:asp.Net 来源:网络整理
导读:我的网址如下所示: customer/login?ReturnUrl=home 在登录视图中,我使用了这种可以正常工作的代码模式。 @using(Html.BeginForm()){ ...} 这神奇地生成以下html form action="customer/login?ReturnUrl=home" method="post" 但现在,我需要在表单中添加一
我的网址如下所示:
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”});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读