过帐后数据不会显示在视图中

时间:2012-07-11 20:06:41

标签: asp.net-mvc model-view-controller

我使用MVC 3,我有一个View来删除我的数据库中的元素。服务层在授权操作之前检查所有逻辑是否到位。如果出现角色违规,则会在Html.Summary中显示一条消息,因此删除操作将中止。

我的问题是当来自服务层的Html.Summary中的消息到达View时丢失了各个字段的数据,因此您无法看到该项的ID,名称和描述。

你能指出我正确的方向吗?我做错了什么?感谢

public ActionResult Delete(int id)
    {
        Typology typology = _typologytService.FindById(id);
        TypologyDeleteViewModel deleteViewModel = Mapper.Map<Typology, TypologyDeleteViewModel>(typology);
        return View(deleteViewModel);
    }

    //
    // POST: /ManageTypologies/Delete/5

    [HttpPost]
    public ActionResult Delete(TypologyDeleteViewModel deleteViewModel)
    {
        if (ModelState.IsValid)
        {
            Typology typology = Mapper.Map<TypologyDeleteViewModel, Typology>(deleteViewModel);
            _typologytService.Delete(typology.TypologyId);
            if (ModelState.IsValid)
                return RedirectToAction("Index");
        }
        return View();         
    }

@model xxx.ViewModels.TypologyDeleteViewModel
@{
    ViewBag.Title = "Delete";
}
<h2>
    Delete</h2>
<h3>
    Are you sure you want to delete this?</h3>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.TypologyId)
    <fieldset>
        <legend>TypologyDeleteViewModel</legend>
        <div class="display-label">
            TypologyId</div>
        <div class="display-field">
            @Html.DisplayFor(model => model.TypologyId)
        </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>
        <p>
            <input type="submit" value="Delete" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

1 个答案:

答案 0 :(得分:1)

将您的帖子方法更改为:

[HttpPost]
    public ActionResult Delete(TypologyDeleteViewModel deleteViewModel)
    {
        if (ModelState.IsValid)
        {
            Typology typology = Mapper.Map<TypologyDeleteViewModel, Typology>(deleteViewModel);
            _typologytService.Delete(typology.TypologyId);
            if (ModelState.IsValid)
                return RedirectToAction("Index");
        }
        return View(deleteViewModel);         
    }

当您返回无效的模型状态时,您没有将模型传递回视图。另外,有没有理由在上面的代码中检查ModelState.IsValid属性两次?我没有看到你可能在哪里添加ModelStateError,并且没有第二个if语句的路径。

最后,如果你的deleteViewModel包含任何下拉列表等(在这种情况下你似乎没有)......你需要在返回视图之前重新填充它们。

修改

在您的情况下,由于您没有为其他属性使用EditorFor HTML帮助程序,因此上述操作无效。您的视图正在吐出相当于<span>标签的描述等,这些标签未发布回服务器。返回的唯一内容是您的Id属性,因为它位于<input type="hidden"字段中。

所以 - 在您的情况下,您需要将操作方法​​更新为:

   [HttpPost]
    public ActionResult Delete(TypologyDeleteViewModel deleteViewModel)
    {
        if (ModelState.IsValid)
        {
            Typology typology = Mapper.Map<TypologyDeleteViewModel, Typology>(deleteViewModel);
            _typologytService.Delete(typology.TypologyId);
            if (ModelState.IsValid)
                return RedirectToAction("Index");
        }
        Typology typology = _typologytService.FindById(typology.TypologyId);
        TypologyDeleteViewModel deleteViewModel = Mapper.Map<Typology, TypologyDeleteViewModel>(typology);
        return View(deleteViewModel);         
    }

或者,将您的值存储在隐藏的输入字段中,就像使用您的ID一样,以便将所有值发布回服务器,而不必再次进行数据库调用。