单击Edite按钮时未执行任何操作

时间:2016-10-10 12:11:11

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

我试图从我的索引视图中编辑一行(我的项目是一个简单的电话簿),它显示了我的所有记录(联系人)但是当我点击编辑按钮时没有任何反应

这是我的删除方法

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

这是它的观点(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>

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:-1)

在您的主页/索引视图中,链接指向HomeController的编辑操作

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>