我使用 MVC 3,我有一个控制器,代码如下?
// GET: /ManageAdvSlots/Delete/5
public ActionResult Delete(int id)
{
AdvSlot advSlot = advSlotRepository.FindById(id);
AdvSlotDeleteViewModel deleteViewModel = Mapper.Map<AdvSlot, AdvSlotDeleteViewModel>(advSlot);
return View(deleteViewModel);
}
//
// POST: /ManageAdvSlots/Delete/5
[HttpPost]
public ActionResult Delete(AdvSlotDeleteViewModel deleteViewModel)
{
if (ModelState.IsValid)
{
AdvSlot advSlot = Mapper.Map<AdvSlotDeleteViewModel, AdvSlot>(deleteViewModel);
advSlotRepository.Delete(advSlot.AdvSlotId);
advSlotRepository.Save();
}
return RedirectToAction("Index");
}
}
看法
@model MyProject.ViewModels.AdvSlotDeleteViewModel
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>AdvSlotDeleteViewModel</legend>
<div class="display-label">AdvSlotId</div>
<div class="display-field">
@Html.DisplayFor(model => model.AdvSlotId)
</div>
<div class="display-label">Name</div>
<div class="display-field">
@Html.DisplayFor(model => model.Name)
</div>
<div class="display-label">Description</div>
<div class="display-field">
@Html.DisplayFor(model => model.Description)
</div>
<div class="display-label">IsPublished</div>
<div class="display-field">
@Html.DisplayFor(model => model.IsPublished)
</div>
<div class="display-label">Code</div>
<div class="display-field">
@Html.DisplayFor(model => model.Code)
</div>
<div class="display-label">Note</div>
<div class="display-field">
@Html.DisplayFor(model => model.Note)
</div>
</fieldset>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "Index")
</p>
}
查看模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyProject.ViewModels
{
public class AdvSlotDeleteViewModel
{
public int AdvSlotId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool IsPublished { get; set; }
public string Code { get; set; }
public string Note { get; set; }
}
}