我有一个剃刀视图,我在一个'if'语句中添加了一个删除按钮,当在浏览器中呈现视图时,它在删除按钮旁边显示"System.Web.Mvc.Html.MvcForm".
我怎么摆脱它?
这是代码:
<div id="deletestatusupdate">
@if (update.User.UserName.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase))
{
@Html.BeginForm("deleteupdate", "home")
@Html.Hidden("returnUrl", Request.Url.ToString())
<button name="id" value="@update.StatusUpdateId">Delete</button>
}
</div>
Run Code Online (Sandbox Code Playgroud)
以下是它在渲染的Razor视图中的显示方式:
System.Web.Mvc.Html.MvcForm [删除按钮]
假装[删除按钮]是一个实际的按钮,不喜欢拍摄屏幕.
谢谢您的帮助.
我们为什么需要使用@Html.AntiForgeryToken()?我搜索但没有得到满意的答案.
我的网址看起来像这样:
customer/login?ReturnUrl=home
Run Code Online (Sandbox Code Playgroud)
在登录视图中,我使用了这种代码模式,它工作正常.
@using(Html.BeginForm())
{
...
}
Run Code Online (Sandbox Code Playgroud)
这神奇地生成以下html
<form action="customer/login?ReturnUrl=home" method="post">
Run Code Online (Sandbox Code Playgroud)
但现在,我需要data-id="something"在表单中添加一个属性(例如).我怎样才能做到这一点?如果我没有任何查询字符串,我知道我可以这样做:
@using(Html.BeginForm(action, controller, FormMethod.Post,
new { data_id="something" }))
Run Code Online (Sandbox Code Playgroud)
但是不知道如何添加应该在html中的查询字符串:
<form action="customer/login?ReturnUrl=home" method="post" data-id="something">
Run Code Online (Sandbox Code Playgroud)
我想过<form>直接使用但不知道如何指定可变的查询字符串.我不知道如何实现它Html.BeginForm.任何提示将不胜感激.
解析度:
现在,我使用<form>以下提示如何获取View中的当前url值.结果视图看起来像
<form action="@Request.Url.PathAndQuery" data-id="something" method="POST">
Run Code Online (Sandbox Code Playgroud)
但是有一个重载的方法是很好的BeginForm.
我使用以下内容创建表单以在移动网站上传图像.
@using (Html.BeginForm("Form/", "Quote", FormMethod.Post, new { enctype = "multipart/form-data" }))
Run Code Online (Sandbox Code Playgroud)
然而,由于它使用的是jQuery mobile,我启用了Ajax,因此页面之间的转换非常顺畅.这导致了我的表单无法上传图像的问题,因为您无法使用ajax进行文件上传.我需要将该属性添加data-ajax="false"到此表单,以便允许我上传文件.
有没有人知道我是如何做到这一点的,因为我尝试了以下的多种变体,但无法让它工作:
@using (Html.BeginForm("Form/", "Quote", FormMethod.Post, new { enctype = "multipart/form-data", "data-ajax" = "false" }))
Run Code Online (Sandbox Code Playgroud) 我喜欢清洁
using (Html.BeginForm())
Run Code Online (Sandbox Code Playgroud)
并且讨厌添加HTML属性需要指定控制器,操作和表单方法.
using (Html.BeginForm("Action", "Controller", FormMethod.Post,
new { id = "inactivate-form" })
Run Code Online (Sandbox Code Playgroud)
有没有办法使用Html.BeginForm和指定表单的HTML属性,而无需手动连接其他所有内容?
我想知道是否有可能创建一个具有类似于Html.BeginForm()的功能和行为的扩展方法,因为它会生成一个完整的Html标记,我可以在<% { & } %>标记内具体说明其内容.
例如,我可以有一个视图:
<% using(Html.BeginDiv("divId")) %>
<% { %>
<!-- Form content goes here -->
<% } %>
Run Code Online (Sandbox Code Playgroud)
在我尝试使用此问题中的示例生成的功能的上下文中,此功能非常有用
这将使我能够为我将要的类型创建容器
<% var myType = new MyType(123, 234); %>
<% var tag = new TagBuilder("div"); %>
<% using(Html.BeginDiv<MyType>(myType, tag) %>
<% { %>
<!-- controls used for the configuration of MyType -->
<!-- represented in the context of a HTML element, e.g.: -->
<div class="MyType" prop1="123" prop2="234">
<!-- add a select here -->
<!-- …Run Code Online (Sandbox Code Playgroud) 我在一个叫做的区域有一个控制器 Admin
public class SiteVisitController : Controller
{
public ViewResult ReadyForCompletion() { ... }
public ViewResult CompleteAndExport() { ... }
}
Run Code Online (Sandbox Code Playgroud)
和一个view(ReadyForCompletion.cshtml),它回发到同一个类上的不同控制器动作
@using (Html.BeginForm( "CompleteAndExport", "SiteVisit" ))
{
<input type="submit" value="Complete & Export" />
}
Run Code Online (Sandbox Code Playgroud)
生成的此表单的HTML有一个空白操作:
<form action="" method="post"> <input type="submit" value="Complete & Export" />
</form>
Run Code Online (Sandbox Code Playgroud)
我想知道为什么这有空白动作?有关更多信息,我还添加了一个
@Url.RouteUrl(new { controller = "ReadyForCompletion", action = "SiteVisit", area = "Admin" })
Run Code Online (Sandbox Code Playgroud)
这也打印出一个空字符串.此外,如果我使用空,Html.BeginForm()它会生成正确的操作.
注册路线是
context.MapRoute(
"Admin_manyParams",
"Admin/{controller}/{action}/{id}/{actionId}",
new { action = "Index", id = UrlParameter.Optional, actionId = …Run Code Online (Sandbox Code Playgroud) 我有一个类似于块的自定义实现Html.BeginForm().实施基本如下:
public class MyBlock : IDisposable {
private readonly HtmlHelper _html;
public MyBlock(HtmlHelper hml) {
this._html.ViewContext.Writer.WriteLine("BEGIN");
}
public void Dispose() {
this._html.ViewContext.Writer.WriteLine("END");
}
}
Run Code Online (Sandbox Code Playgroud)
然后在我看来,我可以这样做:
@using (new MyBlock(Html)) {
@: some content
}
Run Code Online (Sandbox Code Playgroud)
要得到:
BEGIN
some content
END
Run Code Online (Sandbox Code Playgroud)
一切正常.但是,当我在"razor片段"中使用我的块时遇到麻烦,例如将一些剃刀内容传递给一个Func<object, HelperResult>以参数为参数的函数.例如,我有另一个HtmlHelper函数定义如下:
public static IHtmlString Content(this HtmlHelper @this, Func<object, HelperResult> razor) {
return razor(null);
}
@* use in a view as: *@
@{
var razorContent = Html.Content(@<div>Some razor content</div>);
}
@razorContent
Run Code Online (Sandbox Code Playgroud)
但是,当我执行以下操作时,内部内容呈现时没有外部内容:
@{
var content =Html.Content(
@<text>
@using …Run Code Online (Sandbox Code Playgroud) 我正在编写一个自定义验证集,它将显示div上所有缺少的元素.我希望能够使用一个自定义@Html.BeginForm()方法来写出div,但我真的不知道从哪里开始,因为这个坚果比一个写出标签或字符串的html扩展更难解决(表单封装数据/控件,}最后关闭).
我查看了内置BeginForm()方法的元数据版本,这对我没什么帮助.从本质上讲,我只是想,如果能够延长该法,并把它写出来MvcHtmlString的div,这将是显示/ JavaScript中隐藏.
最终我正在努力的是弄清楚如何编写这个具有开始和结束组件的自定义助手.
@using(Html.BeginForm())
{
...
}
Run Code Online (Sandbox Code Playgroud)
我希望能够做到这样的事情:
@using(Html.VBeginForm())
{
...
}
Run Code Online (Sandbox Code Playgroud)
并有渲染我的额外HTML
编辑:从下面的建议添加代码
public class VBeginForm : IDisposable
{
private readonly HtmlHelper _helper;
public VBeginForm(HtmlHelper htmlHelper, string areaName)
{
_helper = htmlHelper;
var container = new TagBuilder("form");
container.GenerateId(areaName);
var writer = _helper.ViewContext.Writer;
writer.Write(container.ToString(TagRenderMode.StartTag));
}
public void Dispose()
{
_helper.ViewContext.Writer.Write("</form>");
}
}
Run Code Online (Sandbox Code Playgroud) 这似乎是一种愚蠢的努力,但这是我想要学习的东西.
现在,在ASP.NET MVC 3.0中,你需要使用语法@using (Html.BeginForm()) {然后,}关闭一个表单块以获得新的'不引人注目的javascript',以免你想手工编写所有这些(这很好) ).
出于某种原因(Read: *OCD*)我不喜欢这样.我真的宁愿这样做..
@Html.BeginForm()
<div class="happy-css">
</div>
@Html.EndForm()
Run Code Online (Sandbox Code Playgroud)
好像傻了吗?是的,对每个人都很好.我想知道为什么它如何工作并根据自己的喜好塑造它.所以我认为我开始挖掘的第一个地方是MVC 3.0源代码本身.所以我跳进了codeplex来找到BeginFormExtension方法.
(http://aspnet.codeplex.com/SourceControl/changeset/view/63452#288009)
所以现在我对如何开始实现目标感到有些困惑.通过阅读代码,我发现它们都归结为根方法(这并不奇怪,因为大多数扩展方法似乎都是分层方法,所有这些方法都是为了避免冗余而分成单个方法).
private static MvcForm FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes) {
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);
HttpResponseBase httpResponse = htmlHelper.ViewContext.HttpContext.Response;
httpResponse.Write(tagBuilder.ToString(TagRenderMode.StartTag)); …Run Code Online (Sandbox Code Playgroud) html-helper unobtrusive-javascript html.beginform asp.net-mvc-3
html.beginform ×10
asp.net-mvc ×5
razor ×3
c# ×2
html ×1
html-helper ×1
idisposable ×1
xhtml ×1