0

这可能是一个愚蠢的错误,但我花了很长时间来解决这个问题。

我有一个名为 users 的现有表,我添加了新的列名为 Approve (bit)。在我的表单编辑上,我试图提供复选框来批准用户进行授权。

下面是我的表格:

<%@ Control Language="C#"    Inherits="System.Web.Mvc.ViewUserControl<EmsAdmin.Models.User>" %>
<%@ Import Namespace="EmsAdmin.Models" %>
 <script type="text/javascript">
//<![CDATA[
// Focus on the first input box for the site
$(document).ready(function () {
    $('#UserId').focus();
});

//]]>
</script>

< %= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try
         again.") %>
<% using (Html.BeginForm())
 {%>

 <%= Html.AntiForgeryToken() %>

 <p>
<%= Html.LabelFor(e=>e.Id,"Id:") %>
<%: Model.Id %>

</p>
<p>

<%= Html.LabelFor(e=>e.PersonId,"Person:") %>
  <%= Html.DropDownListFor(e =>
  e.PersonId, (SelectList)ViewData["allPersons"], "Select
 a person", new { @style = "width: 255px;" })%>
 <%= Html.ValidationMessageFor(e=>e.Person,"") %>
 </p>

 <p>
<%= Html.LabelFor(e=>e.Email,"Email:") %>
<%= Html.TextBoxFor(e => e.Email, new { @style = "width:250px;" })%>
<%= Html.ValidationMessageFor(e=>e.Email,"") %>
 </p>

<%= Html.LabelFor(e=>e.Approve,"Aprrove User:") %>
<%= Html.CheckBoxFor(e=>e.Approve) %><span>&nbsp;Switch this on if you want to
    display whitespace in the widget rather than out of stock logos</span>
<%= Html.ValidationMessageFor(e=>e.Approve,"") %>

我收到以下错误:

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

Source Error: 


Line 29: </p>
Line 30:     <%= Html.LabelFor(e=>e.Approve,"Aprrove User:") %>
Line 31:     <%= Html.CheckBoxFor(e=>(bool) e.Approve) %><span>&nbsp;Switch this on if you want to
Line 32:         display whitespace in the widget rather than out of stock logos</span>
Line 33:     <%= Html.ValidationMessageFor(e=>e.Approve,"") %>

Source File: d:\Dev2010\ems1\Project1\EmsAdmin\Views\ReportUserVerification\UserForm.ascx    Line: 31 

Stack Trace: 


[InvalidOperationException: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.]
   System.Web.Mvc.ModelMetadata.FromLambdaExpression(Expression`1 expression, ViewDataDictionary`1 viewData) +513485
   System.Web.Mvc.Html.InputExtensions.CheckBoxFor(HtmlHelper`1 htmlHelper, Expression`1 expression, IDictionary`2 htmlAttributes) +71
   System.Web.Mvc.Html.InputExtensions.CheckBoxFor(HtmlHelper`1 htmlHelper, Expression`1 expression) +54
   ASP.views_reportuserverification_userform_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in d:\Dev2010\ems1\Project1\EmsAdmin\Views\ReportUserVerification\UserForm.ascx:31
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +268
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +57
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +128
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +41
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +57
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1386

我的用户类:

  using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;

 namespace EmsAdmin.Models
  {
/// <summary>
/// Extends the class created by Linq, so that we can have more control over our linq objects.
/// </summary>
[MetadataType(typeof(UserValidation))]
public partial class User
{
    /// <summary>
    ///  A collection of the Interaction Records for this report
    /// </summary>
    public List<User> Users { get; set; }

    /// <summary>
    /// A collection for displaying brands for the user
    /// </summary>
    public List<Manufacturer> Manufacturers { get; set; }

    /// <summary>
    /// Override the to string so that we can use in the delete method to capture details of this record before it is deleted
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        return string.Format("User: Id = {0}, PersonId = {1}, Name = {2}, Email = {3}, Guid = {4}, CreatedAt = {5}, Salt = {6}, Avatar = {7}, Password = {8}, LastLoggedIn = {9}, LoggedInFrom = {10},Approve = {11}", Id, PersonId, Name, Email, Guid, CreatedAt, Salt, Avatar, Password, LastLoggedIn, LoggedInFrom,Approve);
    }
}

/// <summary>
/// User Validation
/// </summary>
public class UserValidation
{
    /// <summary>
    /// PersonId
    /// </summary>
    [Required(ErrorMessage = "PersonId is required")]
    [Range(1, int.MaxValue, ErrorMessage = "PersonId must be valid")]
    public int PersonId { get; set; }

    /// <summary>
    /// Name
    /// </summary>
    [Required(ErrorMessage = "Name is a required field")]
    [StringLength(128, ErrorMessage = "Name maximum length is 128", MinimumLength = 1)]
    public String Name { get; set; }

    /// <summary>
    /// Email
    /// </summary>
    [Required(ErrorMessage = "Email is a required field")]
    [StringLength(128, ErrorMessage = "Email maximum length is 128", MinimumLength = 1)]
    public String Email { get; set; }

    /// <summary>
    /// Guid
    /// </summary>
    [Required(ErrorMessage = "Guid is a required field")]
    [StringLength(32, ErrorMessage = "Guid maximum length is 32", MinimumLength = 1)]
    public String Guid { get; set; }

    /// <summary>
    /// CreatedAt
    /// </summary>
    [Required(ErrorMessage = "CreatedAt is a required field")]
    public DateTime CreatedAt { get; set; }

    /// <summary>
    /// Salt
    /// </summary>
    [StringLength(9, ErrorMessage = "Salt maximum length is 9")]
    public String Salt { get; set; }

    /// <summary>
    /// Avatar
    /// </summary>
    [StringLength(128, ErrorMessage = "Avatar maximum length is 128")]
    public String Avatar { get; set; }

    /// <summary>
    /// Password
    /// </summary>
    [Required(ErrorMessage = "Password is a required field")]
    [StringLength(512, ErrorMessage = "Password maximum length is 512", MinimumLength = 1)]
    public String Password { get; set; }

    /// <summary>
    /// LoggedInFrom
    /// </summary>
    [StringLength(25, ErrorMessage = "LoggedInFrom maximum length is 25")]
    public String LoggedInFrom { get; set; }

}
}
4

1 回答 1

1

所以,在我们聊天之后,你必须:

  1. 使您的列不可为空。这不是必需的,但是拥有一个 null 批准的用户是没有意义的
  2. 更新您的模型以适应表格的新列public bool Approved { get; set;}
  3. 在您的视图上使用它Html.CheckBoxFor(e=>e.Approve)
于 2013-07-29T16:32:20.840 回答