在MVC中编辑单个项目的视图上的webgrid

时间:2015-02-07 05:59:44

标签: asp.net-mvc asp.net-mvc-4

我正在尝试编辑编辑视图中的某个项目详细信息,并且某些注释与该项目相关。我想在编辑视图中显示详细信息,并在项目相关评论的网格中显示。但它显示错误,我不明白如何解决它。

我的模特:

[Table("tblErrorUpdate")]
public class ErrorUpdate
{
    [Key]
    public int EUID { get; set; }

    [Required(ErrorMessage = "Please Select The Project.")]
    public string Project { get; set; }

    [Required(ErrorMessage = "Please Select Type.")]
    public string Type { get; set; }

    [Required(ErrorMessage = "Header is Required.")]
    [StringLength(100, MinimumLength = 10, ErrorMessage = "Header Should have minimum 10 characters.")]
    public string Header { get; set; }

    [Required(ErrorMessage = "Description is Required.")]
    [StringLength(1000, MinimumLength = 20, ErrorMessage = "Description Should have minimum 20 characters.")]
    public string Description { get; set; }

    public int? EnteredbyID { get; set; }

    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime? CreatedOn { get; set; }

    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime? LastedUpdateOn { get; set; }

    public int? ModifiedBy { get; set; }

    public bool Live { get; set; }

    public IEnumerable<UserComments> UserCommentsList { get; set; }

}

[Table("tblComments")]
public class UserComments
{
    [Key]
    public int id { get; set; }
    [Required( ErrorMessage="Comment is Reqiured")]
    [StringLength(500,MinimumLength=20,ErrorMessage="You Can Enter Minimum 20 character and maximun 500 character")]
   public string Comment { get; set; }
   public int ByID { get; set; }
[DisplayFormat(DataFormatString="{0:MM/dd/yyyy}")]
   public DateTime? CommentDate { get; set; }
   public Boolean? Live { get; set; }
   public int CommentID { get; set; }
}

我的观点:

@using (Html.BeginForm("EditEU", "EUList", new { id = @Model.EUID }, FormMethod.Post))
{
  <div class="row">
    <div class="col-md-6">      
      <div class="alert alert-info">
        <h4>Please Fill Details Carefully :</h4>
        @Html.ValidationSummary()
      </div>
      <label>Select Project </label>
     @Html.DropDownListFor(m=>m.Project, new SelectList (
        from node in XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/XmlFiles/projects.xml")).Descendants("project") 
            select new System.Web.UI.WebControls.ListItem
            (
            node.Element("Text").Value,
            node.Element("Value").Value               
            )                
        ),
        new { @class = "form-control"}
        )
        <label>Selcet Type </label>
     @Html.DropDownListFor(
        m => m.Type,

        new List<SelectListItem>
            {
                new SelectListItem { Text="Error", Value="Error"},
                new SelectListItem { Text="Update", Value="Update"}
            }
        ,
         new { @class = "form-control" }
        )
         <label>Header </label>
        @Html.TextBoxFor(m => m.Header, new { @class = "form-control" })

        <hr />
     <label>Live Status : </label>  @Html.DisplayFor(m=>m.Live)
        <hr />





       @{
        var grid = new WebGrid(source: Model.UserCommentsList, canPage: true, canSort: true , rowsPerPage:10);
        }
      <div class="table-responsive">
         @grid.GetHtml(


            htmlAttributes: new { id = "gridList", @class = "table table-striped table-bordered table-hover" },

    columns: grid.Columns(
    grid.Column(columnName:"id", format:(item)=>"100"+item.id , header:"ID"),
    grid.Column("Comment"),
    grid.Column("ByID"),
    grid.Column("CommentDate", format: (item) => item.CommentDate.ToString("MM/dd/yyyy")),
    grid.Column(header: "Active", columnName: "Live", format: (item) =>
    {
        if ((bool)item.Live == true) { return "Yes"; }
        else
        {
            return "No";
        }
    })
    ))


    </div>

</div>

我的控制者:

[ActionName("EditEU")]
public ActionResult EditEUPost(int id)
{ 
  Basic bs=new Basic();
  int uid =Convert.ToInt32(bs.Decrypt(Session["cookieUID"].ToString()));
  ConnectionContext connectionContext = new ConnectionContext();
  ErrorUpdate data = connectionContext.ErrorUpdates.SingleOrDefault(x => x.EUID == id);
  TryUpdateModel(data);
  if (ModelState.IsValid)
  {
    try
    {
      data.ModifiedBy = uid;
      data.LastedUpdateOn = System.DateTime.Now.Date;
      connectionContext.SaveChanges();
      return RedirectToAction("SuccessUser", "Messages");
    }
    catch
    {
      return RedirectToAction("ErrorUser", "Messages");
    }
  }
  return View();
}

ERROR:

  

必须先绑定数据源才能执行此操作。

可能性: 我做错了什么,实际上它正在做的事情,如果我从视图中删除webgrid代码,我是webgrid的新手,所以不知道这个网格有什么问题。 而且,数据库表具有注释数据。 编辑时的网址:http://localhost:51823/EUlist/EditEU/4

0 个答案:

没有答案