Asp.net mvc MVC中编辑单个项目视图上的webgrid
我试图在编辑视图中编辑某个项目的详细信息,并且某些注释与该项目相关。我想在编辑视图和项目相关评论的web网格中显示详细信息。但它显示出错误,我不知道如何解决它 我的模型:Asp.net mvc MVC中编辑单个项目视图上的webgrid,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我试图在编辑视图中编辑某个项目的详细信息,并且某些注释与该项目相关。我想在编辑视图和项目相关评论的web网格中显示详细信息。但它显示出错误,我不知道如何解决它 我的模型: [Table("tblErrorUpdate")] public class ErrorUpdate { [Key] public int EUID { get; set; } [Required(ErrorMessage = "Please Select The Project.")] pu
[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; }
}
错误:
必须先绑定数据源,然后才能执行此操作
可能性:
我做错了什么,事实上,如果我从视图中删除webgrid代码,并且我是webgrid的新手,那么我不知道这个webgrid有什么问题。
此外,数据库表包含用于注释的数据。
编辑时的URL:请向我显示将显示编辑表单的操作?要显示的操作:公共操作结果EditEU(int-id){ConnectionContext ConnectionContext=new ConnectionContext();var data=(从ConnectionContext.ErrorUpdates中的p开始,其中p.EUID==id选择p)。SingleOrDefault();return View(data);}您只是将集合对象发送到视图,但在数据源中使用的“Model.UserCommentsList”无效。仅对数据源使用模型。如果要在当前情况下使用代码,请创建一个viewmodel,然后创建一个名为UserCommentsList的集合,然后将数据库中的记录设置为该集合,并返回viewmodel,如return this.view(viewmodel)。如何传递模型?我正在编辑一个项目,这就是为什么我使用单个id传递数据。
@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();
}