javascript - 如何在同一模型的同一MVC页面上重用表单?

标签 javascript jquery asp.net-mvc forms

我建立了一个带有简单联系表单的 View ,其中包含姓名、电子邮件和电话。我在同一父页面上重复使用此表单两到三次。当用户提交其中一个表单时,我检测提交的表单并使用 Dapper 通过 javascript/jQuery 将内容发布到数据库。 .

问题是当我在任何页面上重复使用联系表单时,输入字段将不会使用唯一 ID 生成。这会导致 w3 验证失败并显示Duplicate ID。在这种特殊情况下,我需要页面通过 w3 验证。

我该如何解决这个问题?我尝试使用 ViewData.TemplateInfo.HtmlFieldPrefix 为输入字段添加前缀,但并没有真正解决问题,因为前缀值是静态的。如果我使用随机前缀值,那么如何在 HttpPost Controller 中捕获它?

这是我的代码。

索引.cshtml - 多次引用 ContactForm.cshtml:

<html>
<head>...</head>
<body>
....
 @{Html.RenderAction("ContactForm")}
....
 @{Html.RenderAction("ContactForm")}
....
</body>
</html>

ContactForm.cshtml

@model ProjectX.Models.CaseModel
@using (Html.BeginForm("SendMessage", "Home", FormMethod.Post))
{
    @Html.TextInputFor(model => model.Name, Res.Name, new { placeholder = Res.Name } )
    @Html.TextInputFor(model => model.Phone, Res.Phone, new { placeholder = Res.Phone, type = "tel" })
    @Html.TextInputFor(model => model.Email, Res.Email, new { placeholder = Res.Email, type = "email" })
    <input type="submit" value="Send" onclick="jsSaveForm(event);" />
}

// @Html.TextInputFor MvcHtmlString helper method 
public static MvcHtmlString TextInputFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string title, Object htmlAttributes = null)
{
    var req = ModelMetadata.FromLambdaExpression(expression, helper.ViewData).IsRequired;
    var name = helper.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression));
    if (req)
        title += " *";

    string html = String.Format("<div class=\"inp\"><label for=\"{2}\">{0}</label>{1}</div>", helper.ValidationMessageFor(expression), helper.TextBoxFor(expression, htmlAttributes), name);
    return new MvcHtmlString(html);
}

CaseModel.cs

public class CaseModel
{
    [Required(ErrorMessageResourceType = typeof(Res), ErrorMessageResourceName = "ValidationRequired")]
    public string Name { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
}

家庭 Controller

// GET
[ChildActionOnly]
[Route("~/Home/ContactForm")]
public ActionResult ContactForm()
{
    // tried this but this is a static value. 
    // ViewData.TemplateInfo.HtmlFieldPrefix = "SomePrefix";

    return PartialView(new CaseModel());
}

// POST
[HttpPost]
[Route("~/Home/SendMessage")]
public async Task<PartialViewResult> SendMessage(CaseModel model)
{
   ... brevity...
   await SaveData(model);
}

最佳答案

例如,您可以通过在 htmlAttributes 参数中将 id 属性设置为空字符串来删除它

@Html.TextInputFor(model => model.Name, Res.Name, new { id = "", placeholder = Res.Name } )

尽管您可能需要考虑在 TextInputFor() 扩展方法中执行此操作。

顺便说一句,您的扩展方法不会考虑属性的显示名称(使用 [Display(Name = "...")] 属性时,以及 title 参数是不必要的。我建议你的代码应该是

public static MvcHtmlString TextInputFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
{
    ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
    string displayName = metaData.DisplayName;
    if (metaData.IsRequired)
    {
        displayName += " *";
    }
    StringBuilder html = new StringBuilder();
    html.Append(helper.LabelFor(expression, displayName).ToString());
    html.Append(helper.TextBoxFor(expression, htmlAttributes).ToString());
    html.Append(helper.ValidationMessageFor(expression).ToString());
    TagBuilder container = new TagBuilder("div");
    container.AddCssClass("inp");
    container.InnerHtml = html.ToString();
    return new MvcHtmlString(container.ToString());
}

关于javascript - 如何在同一模型的同一MVC页面上重用表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46858964/

相关文章:

javascript - 同一页面上的 Jquery 和 Captcha 无法正常工作

javascript - 使用 React 和 Material UI 创建动态标题 UI

asp.net-mvc - 为剑道网格列绑定(bind)客户端模板中的两个字段

javascript - React 构造函数声明 props 崩溃智能感知 vscode

javascript - 在表内的下拉列表中选择值时查找行索引

javascript - 当用户点击播放视频时使计时器停止

c# - ASP.NET MVC 中的自定义安全方案

javascript - 当用户输入 "completes"而不是 keyup 时发送 XHR

javascript - 如何迭代 Knockout ObservableArray? (长度为 0)

asp.net-mvc - MVC ViewData 是如何工作的?