【问题标题】:No action occurred when clicking on Edite button单击编辑按钮时未发生任何操作
【发布时间】: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() 方法。你Index view 和它有什么关系?
  • 这是一个显示我所有记录的视图
  • 单击“详细信息”按钮时会发生什么?它是有效的还是像编辑一样?

标签: c# html asp.net-mvc crud


【解决方案1】:

在 HomeController 的 Edit 方法中,试试这个:

[HttpGet]
public ActionResult Edit(int? _id)
{
    if (_id==null)
    {
         return new HttpStatusCodeResult(HttpStatusCode.NoContent); 
    }
    else
    {
        return View();
    }
}

【讨论】:

  • 编辑前需要显示数据吗?与否
  • 是的。如果您想这样做,请从您的数据库中获取您的联系人信息并将其发送到您的视图。
  • 我该怎么做?我的项目是 3 层(模型、视图模型、视图)
  • 您的控制器中有数据库连接吗?
  • 那你从哪里得到你的联系方式?
【解决方案2】:

从您的主页/索引视图中创建一个链接到您的 HomeController 的 edit 操作

Index.cshtml

@model IEnumerable<Phone_Book.ViewModel.DTO.Contact>
<h1>Welcome!</h1>

@{
    ViewBag.Title = "Home";

}
<table>
    <thead>
    <tr>
        <th>Num</th>
        <th>FirstName</th>
        <th>LastName</th>
        <th>Email</th>
        <th>Address</th>
        <th>Actions</th>
    </tr>
    </thead>
    <tbody>
    @foreach (var contact in Model)
    {
        <tr>
            <td>@contact.Num</td>
            <td>@contact.FName</td>
            <td>@contact.LName</td>
            <td>@contact.Address</td>
            <td>@Html.ActionLink("Edit", "Edit", new {id = contact.Id}) </td>
        </tr>
    }
    </tbody>
</table>   

【讨论】:

    猜你喜欢
    • 2011-10-07
    • 2017-12-31
    • 2023-03-27
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    相关资源
    最近更新 更多