对象模型未传递给控制器

时间:2017-01-23 07:11:39

标签: c# asp.net-mvc

我处境很奇怪。

我的对象模型没有传递给我的控制器,我不明白为什么,真的。我会附上代码,也许有一个错误导致错误。

代码:

型号:

public class Article
{
    [Key]
    public int ArticleId { get; set; }

    [Required(ErrorMessage = "Title is required")]
    [Display(Name ="Title")]
    [MinLength(5, ErrorMessage = "Title must have at least 5 characters")]
    public string Title { get; set; }

    [Required(ErrorMessage = "Link is required")]
    [Display(Name = "Link")]
    [MinLength(5, ErrorMessage = "Link must have at least 5 characters")]
    public string Link { get; set; }

    [Display(Name = "Source")]
    [MinLength(3, ErrorMessage = "Source must have at least 3 characters")]
    public string Source { get; set; }

    [Display(Name = "Is read")]
    public bool IsRead { get; set; }

    [Display(Name = "Notes")]
    [DataType(DataType.MultilineText)]
    [MinLength(5, ErrorMessage = "Notes field must have at least 5 characters")]
    public string Notes { get; set; }

    public int UserId { get; set; }
}

查看:

@model CarrerTrack.Web.Model.Article

@{
    ViewBag.Title = "Delete";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<br />

<h3>Are you sure you want to delete this?</h3>
<div>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Title)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Title)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Link)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Link)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Source)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Source)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.IsRead)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.IsRead)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Notes)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Notes)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.UserId)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.UserId)
        </dd>

    </dl>

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        <div class="btn-group" role="group" aria-label="...">
            <input type="submit" value="Delete" class="btn btn-danger" /> 
            @Html.ActionLink("Back to list", "Index", null, new { @class = "btn btn-default" })
        </div>
    }
</div>

控制器(部分):

[HttpPost]
public ActionResult Delete(Model.Article article)
{
    var _article = _articleReadApp.GetById(article.ArticleId);
    _articleCommandApp.Remove(_article.ArticleId);
    return RedirectToAction("Index","Article");
}

请注意编辑(Post wo enter image description here rks),删除(发布)不会。

我对ASP.NET MVC有一些经验。这个超越我,我需要你的帮助。这似乎很容易,但我找不到解决方案,虽然这个问题已经被问到here并且网上有很多教程。我删除了View删除并创建了另一个。仍然没有成功。有什么帮助吗?

2 个答案:

答案 0 :(得分:2)

您需要在隐藏字段中传递数据。你的观点应该是这样的:

@model CarrerTrack.Web.Model.Article

@{
   ViewBag.Title = "Delete";
   Layout = "~/Views/Shared/_Layout.cshtml";   }

 <br />

 <h3>Are you sure you want to delete this?</h3>
 <div>
   <hr />
   <dl class="dl-horizontal">
       <dt>
        @Html.DisplayNameFor(model => model.Title)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Title)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Link)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Link)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Source)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Source)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.IsRead)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.IsRead)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Notes)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Notes)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.UserId)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.UserId)
    </dd>

</dl>

@using (Html.BeginForm())
{
        @Html.AntiForgeryToken()
        @Html.HiddenFor(model => model.Title)
        @Html.HiddenFor(model => model.Link)
        @Html.HiddenFor(model => model.Source)
        @Html.HiddenFor(model => model.IsRead)
        @Html.HiddenFor(model => model.Notes)
        @Html.HiddenFor(model => model.UserId)
        @Html.HiddenFor(model => model.ArticleId)
    <div class="btn-group" role="group" aria-label="...">
        <input type="submit" value="Delete" class="btn btn-danger" /> 
        @Html.ActionLink("Back to list", "Index", null, new { @class = "btn       btn-default" })
    </div>
}

答案 1 :(得分:1)

只发送表单输入,并且可以通过绑定器传递给模型对象。

如果您将@Html.DisplayFor(model => model.XXX)更改为@Html.EditorFor(model => model.XXX)并将所有内容都放在@using (Html.BeginForm()) 内,那么它应该有效。您可以输入readonly \ disabled来模仿DisplayFor行为。