ASP.NET MVC - 停止 Html.TextBoxFor 将点添加到 Name 属性

发布于 2024-09-07 01:07:56 字数 1174 浏览 1 评论 0原文

我对 ASP.NET MVC 相当陌生,并且 Html.TextBoxFor() 遇到了问题 - 它为渲染输入的 name 属性提供了一个点。

<%= Html.TextBoxFor(m => m.Box.Name, new { id= "txtName" }) %>

呈现以下 HTML:

<input type="text" value="" name="Box.Name" id="txtName">

我正在使用 jQuery 验证插件进行客户端验证。由于 Box.Name 中的点,它无法验证此输入(导致 JavaScript 错误)。

我的模型看起来像这样:

public class Box {
   [Required(ErrorMessage = "Please enter a name")]
   public string Name { get; set; }
   public int BoxMaterialID { get; set; }
}

我的 ViewModel 看起来像这样:

public class BoxViewModel
{
    public Box Box { get; set; }
    public List<BoxMaterial> BoxMaterial { get; set;}
}

我的控制器看起来像这样:

public ActionResult New(FormCollection postData)
{
    Box box = new Box();

    try
    {
        UpdateModel(box, "Box");
        boxService.SaveBox(box);
    }
    catch
    {
        return View(new BoxViewModel(box));
    }

    return RedirectToAction("Index", "Boxes");
}

服务器端验证使用模型上的 DataAnnotations 来工作就像一个魅力。我似乎遇到的唯一问题是客户端验证,因为“。”在名称属性中。

感谢您的帮助!

I'm fairly new to ASP.NET MVC and I've got a problem with Html.TextBoxFor() - it's giving the rendered input's name attribute a dot.

<%= Html.TextBoxFor(m => m.Box.Name, new { id= "txtName" }) %>

renders the following HTML:

<input type="text" value="" name="Box.Name" id="txtName">

I'm using the jQuery Validation plugin for client-side validation. It can't validate this input because of the dot in Box.Name (causes a javascript error).

My Model looks like this:

public class Box {
   [Required(ErrorMessage = "Please enter a name")]
   public string Name { get; set; }
   public int BoxMaterialID { get; set; }
}

My ViewModel looks like this:

public class BoxViewModel
{
    public Box Box { get; set; }
    public List<BoxMaterial> BoxMaterial { get; set;}
}

My Controller looks like this:

public ActionResult New(FormCollection postData)
{
    Box box = new Box();

    try
    {
        UpdateModel(box, "Box");
        boxService.SaveBox(box);
    }
    catch
    {
        return View(new BoxViewModel(box));
    }

    return RedirectToAction("Index", "Boxes");
}

Server-side validation is working like a charm using DataAnnotations on the Model. The only problem I seem to be having is with the client-side validation because of the "." in the name attribute.

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_蜘蛛 2024-09-14 01:07:56

当表单在为了正确填充模型。就验证而言,您可以这样设置:

$("#myForm").validate({
    rules: {
        'Box.Name': {
            required: true
        }
    },
    messages: {
        'Box.Name': {
            required: 'some error message'
        }
    }
});

The dot added to the name is used by the default model binder when the form is submitted in order to correctly populate the model. As far as validation is concerned you could set it like this:

$("#myForm").validate({
    rules: {
        'Box.Name': {
            required: true
        }
    },
    messages: {
        'Box.Name': {
            required: 'some error message'
        }
    }
});
痴意少年 2024-09-14 01:07:56

也尝试指定 name 属性:

<%= Html.TextBoxFor(m => m.Box.Name, new { id= "txtName", name = "txtName" }) %>

Try specifying the name attribute as well:

<%= Html.TextBoxFor(m => m.Box.Name, new { id= "txtName", name = "txtName" }) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文