【发布时间】:2016-10-10 12:11:10
【问题描述】:
我正在尝试从显示我所有记录(联系人)的索引视图中编辑一行(我的项目是一个简单的电话簿),但是当我单击编辑按钮时没有任何反应
这是我的删除方法
#region [- Delete -]
#region [- Get -]
[HttpGet]
// [HttpDelete]
public ActionResult Delete(int? _id, Models.EF_Model.Phone_book _model)
{
return View();
}
#endregion
#region [- Post -]
[HttpPost]
//[HttpDelete]
public ActionResult Delete(Models.EF_Model.Phone_book _Model)
{
if (ModelState.IsValid)
{
Ref_ViewModel = new ViewModel.ViewModel();
Ref_ViewModel.Delete(_Model.Id);
}
else
{
ViewBag.Massage = "Choose a Contact";
}
return View(_Model);
}
#endregion
#endregion
这是我在 Home 控制器中的编辑方法
[HttpGet]
public ActionResult Edit(int? _id)
{
if (_id==null)
{
return new HttpStatusCodeResult(HttpStatusCode.NoContent);
}
else
{
Ref_ViewModel = new ViewModel.ViewModel();
return View(Ref_ViewModel.Select(_id));
}
}
[HttpPost]
public ActionResult Edit(ViewModel.DTO.Contact Ref_Contact)
{
if (ModelState.IsValid)
{
Ref_ViewModel = new ViewModel.ViewModel();
Ref_ViewModel.Edit(Ref_Contact, Ref_Contact.Id);
}
else
{
ViewBag.Message = "Choose a Contact";
}
return View();
}
这是它的视图(Contact 类是一个简单的 DTO 类)
@model Phone_Book.ViewModel.DTO.Contact
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Contact</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Num, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Num, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Num, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
这是我的索引视图
@model IEnumerable<Phone_Book.Models.EF_Model.Phone_book>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.First_Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Last_Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Number)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.First_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Last_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Number)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
【问题讨论】:
-
什么都没有发生是什么意思?数据库中的数据未更新或未调用 POST 上的
Edit方法? -
当我按下编辑按钮时,它应该进入编辑页面,但它会显示索引页面
-
你是什么意思从我的索引视图?您显示的方法和视图适用于
Edit()方法。你Indexview 和它有什么关系? -
这是一个显示我所有记录的视图
-
单击“详细信息”按钮时会发生什么?它是有效的还是像编辑一样?
标签: c# html asp.net-mvc crud