无法在ActionResult中检索ViewModel

时间:2012-07-04 11:38:21

标签: asp.net-mvc asp.net-mvc-3

我使用MVC 3,我有一个带有以下代码的控制器,我无法获取ViewModel [HttpPost] ActionResult删除AdvSlotDeleteViewModel结果为NULL,我有一个类似Action的Action方法,它正在工作,你能帮我解决吗?关于这个问题?

// 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; }
    }
}

2 个答案:

答案 0 :(得分:5)

您需要将所有表单字段放在表单块中。否则他们不会被发布到服务器。

<fieldset>移到@using (Html.BeginForm()) { ->> HERE <<- }

PS。对于删除,您可能只想发布id,而不是整个视图模型。

答案 1 :(得分:1)

人们建议的仅仅是在隐藏的表单字段中包含id:

@using (Html.BeginForm()) {
@Html.HiddenFor(x => x.AdvSlotId)
<p>
    <input type="submit" value="Delete" /> |
    @Html.ActionLink("Back to List", "Index")
</p>
}

您可能会发现您不想再检查模型是否有效。如果您对某些字段进行了高级验证,如果它们不在表单中,则不会回发。例如,如果需要“Name”,它将无法使用上面的代码进行验证,因为只有表单中的字段才会回发到服务器。