[asp.net-mvc]单击编辑按钮不时发生任何行动

标签: Asp.net-MVC HTML C#
发布时间: 2017/2/20 23:55:43
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想从我的索引视图显示所有的我的记录 (联系人),但当我点击编辑按钮,什么都不会发生编辑的行 (我的项目是一个简单的通讯簿)

这是我的删除方法

#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

这是我在我家控制器中的编辑方法

    [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();
    }

这是它的视图 (联系人类是一个简单的 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>

解决方法 1:

在您在您的 HomeController 的编辑方法,试试这个︰

[HttpGet]
public ActionResult Edit(int? _id)
{
    if (_id==null)
    {
         return new HttpStatusCodeResult(HttpStatusCode.NoContent); 
    }
    else
    {
        return View();
    }
}
官方微信
官方QQ群
31647020