MVC3 Razor视图获取System.InvalidOperationException错误

时间:2014-11-07 11:16:12

标签: c# asp.net-mvc-3

MVC3 Razor我正在编辑/更新应用程序,但执行视图获取错误

  

异常详细信息:System.InvalidOperationException:模型项   传入字典的类型   ' PayrollApplication.Models.PeopleService',但是这本词典   需要类型的模型项   ' System.Collections.Generic.IEnumerable`1 [PayrollApplication.Models.tbl_PeopleInfo]'

控制器代码编辑方法



  [HttpGet]
    public ActionResult EditView(PeopleService model, int id)
    {           
        PeopleService updatemodel = new PeopleService();
        SqlConnection cn = new SqlConnection(conStr);
        SqlCommand cmd = new SqlCommand("Select PersonId,Name ,Surname,Email ,Birthdate,Children  From tbl_PeopleInfo Where PersonId=" + id, cn);
        cn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        DataTable Dt = new DataTable();
        //Dt.Load(dr);


        if (dr.Read())
        {

            updatemodel.PersonId = Convert.ToInt32(dr["PersonId"].ToString().Trim());
            updatemodel.Name = dr["Name"].ToString().Trim();
            updatemodel.Surname = dr["Surname"].ToString().Trim();
            updatemodel.Email = dr["Email"].ToString().Trim();
            updatemodel.Birthdate = Convert.ToDateTime(dr["Birthdate"].ToString().Trim());
            updatemodel.Children = Convert.ToInt32(dr["Children"].ToString().Trim());
            updatemodel.PassWord = "";
            updatemodel.UserName = "";
          
        }
        else
        {
            dr.Close();
        }
        dr.Close();
        cn.Close();

        return View(updatemodel);
    }
    [HttpPost]
    public ActionResult EditView(PayrollApplication.Models.PeopleService updatemodel, FormCollection collection, int id)
    {

        if (ModelState.IsValid)
        {
            int _records = updatemodel.Update(updatemodel.Name, updatemodel.Surname, updatemodel.Email, updatemodel.Birthdate, updatemodel.Children, updatemodel.PersonId);
            if (_records > 0)
            {
                return RedirectToAction("EmployeeMaster", "Home");
            }
            else
            {
                ModelState.AddModelError("", "Can Not Update");
            }
        }
        return View(updatemodel);
    }






@model PayrollApplication.Models.PeopleService

@{
    ViewBag.Title = "EditView";
    Layout = "~/Views/Shared/_IndexLayout.cshtml";
}

<h2>EditView</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Class1</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.PersonId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PersonId)
            @Html.ValidationMessageFor(model => model.PersonId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Surname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Surname)
            @Html.ValidationMessageFor(model => model.Surname)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Birthdate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Birthdate)
            @Html.ValidationMessageFor(model => model.Birthdate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Children)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Children)
            @Html.ValidationMessageFor(model => model.Children)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.PassWord)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PassWord)
            @Html.ValidationMessageFor(model => model.PassWord)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.RememberMe)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.RememberMe)
            @Html.ValidationMessageFor(model => model.RememberMe)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
&#13;
&#13;
&#13;

人员服务类

&#13;
&#13;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.ComponentModel.DataAnnotations;
namespace PayrollApplication.Models
{
    public class PeopleService
    {
        #region ConnectionString

        static string conStr = ConfigurationManager.ConnectionStrings["PayrollPeople"].ConnectionString;

        #endregion

        #region Properties

        public int PersonId { get; set; }

        [Required]
        [Display(Name = "First Name")]
        public string Name { get; set; }   


        [Required]
        [Display(Name = "Sur Name")]
        public string Surname { get; set; }

        [Required]
        [Display(Name = "Email id")]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }

        [Required]
        [Display(Name = "Birth Date")]
        public DateTime Birthdate { get; set; }

        [Required]
        [Display(Name = "Children")]
        public int Children { get; set; }

        [Required]
        [Display(Name = "User Name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string PassWord { get; set; }


        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }

        #endregion

        #region Function


        public int Insert(string _Name, string _Surname, string _Email, DateTime _Birthdate, int _Children, string _usr, string _pass)
        {
            SqlConnection cn = new SqlConnection(conStr);
            SqlCommand cmd = new SqlCommand("Insert Into tbl_PeopleInfo(Name,Surname,Email,Birthdate,Children,UserName,PassWord)Values('" + Name.Trim() + "','" + Surname.Trim() + "','" + Email.Trim() + "','" + Birthdate + "'," + Children + ",'" + UserName + "','" + PassWord + "')", cn);
            cn.Open();
            return cmd.ExecuteNonQuery();
        }

        public DataSet GetAllAuthors()
        {
            SqlConnection cn = new SqlConnection(conStr);
            SqlCommand cmd = new SqlCommand("Select PersonId,Name,Surname,Email,Birthdate,Children from tbl_PeopleInfo", cn);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            return ds;
        }

     

        public int Delete(int id)
        {
            SqlConnection cn = new SqlConnection(conStr);
            SqlCommand cmd = new SqlCommand("Delete From tbl_PeopleInfo Where PersonId=" + id, cn);
            cn.Open();
            return cmd.ExecuteNonQuery();
        }

        public int Update(string _Name, string _Surname, string _Email, DateTime _Birthdate, int _Children, int _authorid)
        {
            SqlConnection cn = new SqlConnection(conStr);
            SqlCommand cmd = new SqlCommand("Update tbl_PeopleInfo Set Name='" + Name.Trim() + "',Surname='" + Surname.Trim() + "',Email='" + Email.Trim() + "',Birthdate='" + Birthdate + "',Children=" + Children + "  Where PersonId=" + PersonId, cn);
            cn.Open();
            return cmd.ExecuteNonQuery();
        }


        #endregion       

    }
}
&#13;
&#13;
&#13;

**

  

错误图片   enter image description here

**

  

以下代码为EmployeeMaster视图显示所有人信息后,我点击编辑转到EdiView视图。并获得图像上方错误显示。

以上代码在运行剃刀视图时出现错误

&#13;
&#13;
Controller : 
       [HttpGet]
        public ActionResult EmployeeMaster()
        {
            return View(db.tbl_PeopleInfo.ToList());
        }




EmployeeMaster View:  **tbl_PeopleInfo** is my db table name

@model IEnumerable<PayrollApplication.Models.tbl_PeopleInfo>
@{
    ViewBag.Title = "EmployeeMaster";
    Layout = "~/Views/Shared/_IndexLayout.cshtml";
    
}

<div>
    <ul class="breadcrumb">
        <li><a href="#">Home</a> </li>
        <li><a href="#">Employee</a> </li>
    </ul>
</div>
<div class="row">
    <div class="box col-md-12">
        <div class="box-inner">
            <div class="box-header well" data-original-title="">
                <h2>
                    <i class="glyphicon glyphicon-user"></i>Employee Details
                </h2>
                <div class="box-icon">
                    <a href="#" class="btn btn-setting btn-round btn-default"><i class="glyphicon glyphicon-cog">
                    </i></a><a href="#" class="btn btn-minimize btn-round btn-default"><i class="glyphicon glyphicon-chevron-up">
                        @*   </i></a><a href="#" class="btn btn-close btn-round btn-default"><i class="glyphicon glyphicon-remove">*@
                    </i></a>
                </div>
            </div>
            <div class="box-content">
                <div class="alert alert-info">
                    <td>
                      
                  @*      <a href="#" class="btn btn-info btn-setting">*@
                @*  @Html.ActionLink("Add Employee", "Add")*@
                  <INPUT TYPE="BUTTON" class="btn btn-primary" data-dismiss="modal" VALUE="Add Employee" ONCLICK="window.location.href='/Home/Add'">

                                     </td>
                </div>  
          
                <table class="table table-striped table-bordered bootstrap-datatable datatable responsive">
                    <thead>
                        <tr>
                            <th>
                                PersonId
                            </th>
                            <th>
                                Name
                            </th>
                            <th>
                                Surname
                            </th>
                            <th>
                                Email Id
                            </th>
                            <th>
                                BirthDate
                            </th>
                            <th>
                                Children
                            </th>
                            <th>
                                Operation
                            </th>
                        </tr>
                    </thead>
                  @foreach (var item in Model)
                    {

                       
                        <tr>
                            <td align="center">
                                @Html.DisplayFor(modelItem => item.PersonId)
                            </td>
                            <td align="center">
                                @Html.DisplayFor(modelItem => item.Name)
                            </td>
                            <td align="center">
                                @Html.DisplayFor(modelItem => item.Surname)
                            </td>
                            <td align="center">
                                @Html.DisplayFor(modelItem => item.Email)
                            </td>
                            <td align="center">
                                @Html.DisplayFor(modelItem => item.Birthdate)
                            </td>
                            <td align="center">
                                @Html.DisplayFor(modelItem => item.Children)
                            </td>
                            <td>
                           
                                @Html.ActionLink("Edit", "EditView", new { id = item.PersonId }) | 
                                @Html.ActionLink("Delete", "Delete", new { id = item.PersonId }, new { onclick = "return confirm('Are you sure you wish to delete this Person Information?');" })
                            </td>
                        </tr>
                       

                    }
                </table>
            </div>
        </div>
    </div>
    <!--/span-->
</div>
&#13;
&#13;
&#13;

0 个答案:

没有答案