MVC5 ActionResult模型被传递为空

时间:2014-06-26 11:21:29

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

我已经阅读了很多有关此问题的帖子,但我似乎无法找到适合我的实施的解决方案。我给了MVC另一次尝试(我是一个网络形象的人)。传递给我的ActionResult的模型在应该填充时基本上是空的。我主演了有效的样本,但我找不到任何差异。似乎也无法进行调试。任何指针都将非常感激。

查看:

@model WebApplication1.Models.SiteViewModel
@{
    ViewBag.Title = "Delete your site";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<p>Are you sure you want to delete this site?</p>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(false)

    <ul class="list-group">
        <li class="list-group-item">
            <div class="row">
                <div class="col-md-2"><b>@Html.LabelFor(model => model.Name)</b></div>
                <div class="col-md-6">@Model.Name</div>
            </div>
        </li>
        <li class="list-group-item">
            <div class="row">
                <div class="col-md-2"><b>@Html.LabelFor(model => model.Phase)</b></div>
                <div class="col-md-6">@Model.Phase</div>
            </div>
        </li>
        <li class="list-group-item">
            <div class="row">
                <div class="col-md-2"><b>@Html.LabelFor(model => model.Type)</b></div>
                <div class="col-md-6">@Model.Type</div>
            </div>
        </li>
    </ul>
    <button type="submit" class="btn btn-danger">Delete</button>
    @Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-default" })
}
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

型号:

public class SiteViewModel
{
    public SiteViewModel()
    {
        Services = new List<ServiceModel>();
    }

    public SiteViewModel(SiteModel site)
    {
        this.SiteId = site.SiteId;
        this.Name = site.Name;
        this.Type = site.Type;
        this.Phase = site.Phase;
        this.Services = site.Services;
    }

    public int SiteId { get; set; }

    [Required(ErrorMessage = "Name is required")]
    public string Name { get; set; }
    public SchoolType Type { get; set; }
    public SchoolPhase Phase { get; set; }

    public DateTime? Deleted { get; set; }

    public virtual ICollection<ServiceModel> Services { get; set; }
}

控制器操作:

[HttpPost]
    public ActionResult Delete(SiteViewModel model)
    {
        var site = siteRepository.GetById(model.SiteId);
        if (site == null) { throw new ArgumentException(string.Format("Site with Id [{0}] does not exist", model.SiteId)); }

        try
        {
            siteRepository.SoftDeleteAndSubmit(site);

            base.SetSuccessMessage("The site has been (soft) deleted.");
            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            base.SetErrorMessage("Whoops! Couldn't delete the site. The error was [{0}]", ex.Message);
        }

        return View(model);
    }

谢谢, 克里斯。

1 个答案:

答案 0 :(得分:1)

如果您想要一个完全填充的模型,您需要使用表单元素或表单帮助函数来发布您的数据。

@using(Html.BeginForm())
{
    <input type="text" value="@Model.Name" />
    // or
    Html.TextBoxFor(m => m.Name)
    ...
}

您链接的示例依赖于URL路由规则来匹配模型的参数。因此,您需要将SiteId重命名为Id或添加/修改路由规则。

如果您只需要id,那么我只会传递该参数,因为它会使您的意图更加明显并且不易破坏。

@using(Html.BeginForm())
{
    @Html.HiddenFor(m => m.Id)
    <button type="submit">Delete</button>
}

[HttpPost]
public ActionResult Delete(int id)
{
    var site = siteRepository.GetById(id);
    ...
}