Asp.net mvc ASP.NET MVC-Stop Html.textbox用于向Name属性添加点

Asp.net mvc ASP.NET MVC-Stop Html.textbox用于向Name属性添加点,asp.net-mvc,validation,jquery-validate,Asp.net Mvc,Validation,Jquery Validate,我是ASP.NET MVC的新手,我对Html.TextBoxFor()有一个问题-它给呈现输入的name属性一个点 <%= Html.TextBoxFor(m => m.Box.Name, new { id= "txtName" }) %> 我的ViewModel如下所示: public class Box { [Required(ErrorMessage = "Please enter a name")] public string Name { get; s

我是ASP.NET MVC的新手,我对Html.TextBoxFor()有一个问题-它给呈现输入的name属性一个点

<%= Html.TextBoxFor(m => m.Box.Name, new { id= "txtName" }) %>
我的ViewModel如下所示:

public class Box {
   [Required(ErrorMessage = "Please enter a name")]
   public string Name { get; set; }
   public int BoxMaterialID { get; set; }
}
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");
}
$("#myForm").validate({
    rules: {
        'Box.Name': {
            required: true
        }
    },
    messages: {
        'Box.Name': {
            required: 'some error message'
        }
    }
});
使用模型上的数据注释,服务器端验证工作得非常出色。唯一的问题似乎是客户端验证,因为name属性中有“.”


谢谢你的帮助

同时尝试指定name属性:

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

提交表单时,将使用添加到名称中的点,以便正确填充模型。就验证而言,您可以这样设置:

public class Box {
   [Required(ErrorMessage = "Please enter a name")]
   public string Name { get; set; }
   public int BoxMaterialID { get; set; }
}
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");
}
$("#myForm").validate({
    rules: {
        'Box.Name': {
            required: true
        }
    },
    messages: {
        'Box.Name': {
            required: 'some error message'
        }
    }
});

谢谢你的解释,达林。看来我需要更好地理解模型绑定。在名称中添加引号成功了,现在客户端验证开始工作。干杯我尝试了这个,但name属性仍然设置为“Box.name”。谢谢你的建议。