Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ModelBinder中未通过的某些字段_Asp.net Mvc_Modelbinders - Fatal编程技术网

Asp.net mvc ModelBinder中未通过的某些字段

Asp.net mvc ModelBinder中未通过的某些字段,asp.net-mvc,modelbinders,Asp.net Mvc,Modelbinders,以下是我的控制器的代码: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id, Actor actor) { try { actorRepository.Save(actor); return RedirectToAction("Index"); } catch {

以下是我的控制器的代码:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int id, Actor actor)
    {

        try
        {
            actorRepository.Save(actor);
            return RedirectToAction("Index");
        }
        catch
        {
            return View("Edit");
        }
    }
调用的视图具有强类型为Actor类的部分视图。由于某些原因,有几个字段没有绑定到类。讨论中的字段是Address、Address2和ZipCode。它们在页面上填充,但返回空值。其他领域都有,只是这些领域没有

有什么想法吗?另外,我如何编写一个单元测试来复制这种行为

更新 公共级演员 { 公共字符串MiddleName{get;set;}

    [Required(ErrorMessage = "First Name is Required")]
    [RegularExpression(@"\w*", ErrorMessage = "Last Name Contains Illegal Characters")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Last Name is Required")]
    [RegularExpression(@"\w*", ErrorMessage = "Last Name Contains Illegal Characters")]
    public string LastName { get; set; }

    [DataType(DataType.PhoneNumber, ErrorMessage = "Please Enter a Valid Phone Number")]
    [Required(ErrorMessage = "Phone Number is Required")]
    public string PhoneNumber { get; set; }

    [Required(ErrorMessage = "Address is Required")]
    [RegularExpression(@"\w*", ErrorMessage = "Address Contains Illegal Characters")]
    public string Address { get; set; }

    [RegularExpression(@"\w*", ErrorMessage = "Address2 Contains Illegal Characters")]
    public string Address2 { get; set; }

    [RegularExpression(@"\w*",ErrorMessage = "State Contains Illegal Characters")]
    [Required(ErrorMessage = "State is Required")]
    public string State { get; set; }

    [Required(ErrorMessage = "Zip Code is Required")]
    [RegularExpression("\b[0-9]{5}(?:-[0-9]{4})?\b",ErrorMessage = "Please Enter a Valid Zip Code")]
    public string ZipCode { get; set; }

    [Required(ErrorMessage = "Even in theater, you have to choose a gender")]
    public bool? Gender { get; set; }

    [Required(ErrorMessage = "Cell Phone Number is Required")]
    public string CellPhone { get; set; }

    public int ActorId { get; set; }

    [DataType(DataType.MultilineText, ErrorMessage = "Illegal Characters in Notes")]
    public string Notes { get; set; }

    [Required(ErrorMessage = "Email Address is Required")]
    [DataType(DataType.EmailAddress)]
    public string EMail { get; set; }

    [Required(ErrorMessage = "City Is Required")]
    public string City {get; set;}
}

<fieldset>
<legend>Fields</legend>
<p>
    <label for="MiddleName">MiddleName:</label>
    <%= Html.EditorFor(m=>m.MiddleName) %>
    <%= Html.ValidationMessage("MiddleName", "*") %>
</p>
<p>
    <label for="FirstName">FirstName:</label>
    <%=Html.EditorFor(m=>m.FirstName) %>
    <%= Html.ValidationMessage("FirstName", "*") %>
</p>
<p>
    <label for="LastName">LastName:</label>
    <%= Html.TextBox("LastName", Model.LastName) %>
    <%= Html.ValidationMessage("LastName", "*") %>
</p>
<p>
    <label for="PhoneNumber">PhoneNumber:</label>
    <%= Html.TextBox("PhoneNumber", Model.PhoneNumber) %>
    <%= Html.ValidationMessage("PhoneNumber", "*") %>
</p>
<p>
    <label for="Address">Address:</label>
       <%=Html.EditorFor(m=>m.Address) %>
       <%= Html.ValidationMessage("Address", "*") %>
</p>
<p>
    <label for="Address2">Address2:</label>
       <%=Html.EditorFor(m=>m.Address2) %>
    <%= Html.ValidationMessage("Address2", "*") %>
</p>
<p>
    <label for="State">State:</label>
    <%= Html.TextBox("State", Model.State) %>
    <%= Html.ValidationMessage("State", "*") %>
</p>
<p>
    <label for="ZipCode">ZipCode:</label>
    <%= Html.TextBox("ZipCode", Model.ZipCode) %>
    <%= Html.ValidationMessage("ZipCode", "*") %>
</p>
<p>
    <label for="Gender">Gender:</label>
    <%= Html.TextBox("Gender", Model.Gender) %>
    <%= Html.ValidationMessage("Gender", "*") %>
</p>
<p>
    <label for="CellPhone">CellPhone:</label>
    <%= Html.TextBox("CellPhone", Model.CellPhone) %>
    <%= Html.ValidationMessage("CellPhone", "*") %>
</p>
<p>
    <label for="ActorId">ActorId:</label>
    <%= Html.TextBox("ActorId", Model.ActorId) %>
    <%= Html.ValidationMessage("ActorId", "*") %>
</p>
<p>
    <label for="Notes">Notes:</label>
    <%= Html.TextBox("Notes", Model.Notes) %>
    <%= Html.ValidationMessage("Notes", "*") %>
</p>
<p>
    <label for="EMail">EMail:</label>
    <%= Html.TextBox("EMail", Model.EMail) %>
    <%= Html.ValidationMessage("EMail", "*") %>
</p>
<p>
    <label for="City">City:</label>
    <%= Html.TextBox("City", Model.City) %>
    <%= Html.ValidationMessage("City", "*") %>
</p>
<p>
    <input type="submit" value="Save" />
</p>
</fieldset>
[必需(ErrorMessage=“需要名字”)]
[正则表达式(@“\w*”,ErrorMessage=“姓氏包含非法字符”)]
公共字符串名{get;set;}
[必需(ErrorMessage=“需要姓氏”)]
[正则表达式(@“\w*”,ErrorMessage=“姓氏包含非法字符”)]
公共字符串LastName{get;set;}
[数据类型(DataType.PhoneNumber,ErrorMessage=“请输入有效的电话号码”)]
[必需(ErrorMessage=“需要电话号码”)]
公共字符串PhoneNumber{get;set;}
[必需(ErrorMessage=“Address is Required”)]
[正则表达式(@“\w*”,ErrorMessage=“地址包含非法字符”)]
公共字符串地址{get;set;}
[正则表达式(@“\w*”,ErrorMessage=“Address2包含非法字符”)]
公共字符串地址2{get;set;}
[正则表达式(@“\w*”,ErrorMessage=“状态包含非法字符”)]
[必需(ErrorMessage=“State is Required”)]
公共字符串状态{get;set;}
[必需(ErrorMessage=“需要邮政编码”)]
[RegularExpression(“\b[0-9]{5}(?:-[0-9]{4}”)\b”,ErrorMessage=“请输入有效的邮政编码”)]
公共字符串ZipCode{get;set;}
[必需(ErrorMessage=“即使在剧院,您也必须选择性别”)]
公共布尔?性别{get;set;}
[必需(ErrorMessage=“需要手机号码”)]
公共字符串{get;set;}
公共int ActorId{get;set;}
[数据类型(DataType.multilitext,ErrorMessage=“注释中的非法字符”)]
公共字符串注释{get;set;}
[必需(ErrorMessage=“需要电子邮件地址”)]
[数据类型(数据类型.电子邮件地址)]
公共字符串电子邮件{get;set;}
[必需(ErrorMessage=“需要城市”)]
公共字符串City{get;set;}
}
领域

中间名:
m、 中间名)%%>

名字: m、 名字)%%>

姓氏:

电话号码:

地址: m、 地址)%%>

地址2: m、 地址2)%>

声明:

ZipCode:

性别:

手机:

阿克托里德:

笔记:

电邮:

城市:


以下是测试模型绑定器的方法:

只有您将测试DefaultModelBinder

至于字段,您还没有显示Actor源代码。但是既然您谈到了字段,我想您确实使用了字段-但是ASP.NET MVC模型绑定器不会绑定到字段-只绑定到属性。所以如果你有

public class Actor
{
   public string ZipCode;
}
你需要换成

public class Actor
{
   public string ZipCode { get; set; }
}
另一个原因可能是你

public class Actor
{
   public Address Address { get; set; }
}
并且您使用partial来显示地址;所以它被命名为“ZipCode”,但它必须被命名为“Address.ZipCode”。或者你的地址没有无参数构造函数。。。换句话说,在没有看到源代码的情况下,有太多的猜测


对于更新信息,这是没有意义的。坏字段看起来和好字段一样。但是尝试Html.TextBox而不是EditorFor。尝试使用FireBug(或IE Dev)查看POST数据,检查字段名称和值是否正确。如果他们这样做了-问题在于模型活页夹,如果他们不好/缺席-页面有问题。此外,请尝试在控制器操作中检查ModelState.IsValid以及ModelState错误。

在ASP.MVC的DefaultModelBinder类中,有一个名为(default=true)的ModelMetaData属性将空字符串转换为NULL

您可以通过每个属性上的DataAnnotation对此进行更改,如下所示:

[DisplayFormat(ConvertEmptyStringToNull=false)]
或者,您可以创建自己的ModelBinder并从DefaultModelBinder继承,只需覆盖GetPropertyValue方法即可忽略此设置,如下所示:

ModelBinders.Binders.DefaultBinder=new Delphi.Mvc.KeepStringsBinder();
Add(typeof(ObjectBase),newdelphi.Mvc.KeepStringsBinder())

你能列出你的视图和Actor对象定义吗?是的,Actor是一个POCO类,并且在视图中没有任何异国情调(据我所知)。我使用的是auto属性,它只是实现端字段的语法糖。再过一会儿,我写这篇文章的时候已经很晚了,我没有头脑继续写代码。这些字段最初是Html.TextBox,我把它们切换到EditorFor,看看是否有什么不同。事实并非如此,而且由于它们在页面上使用这两种方法呈现的效果相同,我几乎排除了这一原因。作为检查,我为Add方法创建了一个新的强类型视图,并且在相同的字段中得到了相同的错误。是时候再深入一点了。