Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对象模型未传递给控制器_C#_Asp.net Mvc - Fatal编程技术网

C# 对象模型未传递给控制器

C# 对象模型未传递给控制器,c#,asp.net-mvc,C#,Asp.net Mvc,我的处境很奇怪 我的对象模型没有传递给我的控制器,我真的不明白为什么。我会附上代码,可能是一个打字错误,导致错误 代码: 型号: public class Article { [Key] public int ArticleId { get; set; } [Required(ErrorMessage = "Title is required")] [Display(Name ="Title")] [MinLength(5, ErrorMessage =

我的处境很奇怪

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

代码:

型号:

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; }
}
视图:

请注意,编辑(发布)有效,删除(发布)无效


我有一些ASP.NETMVC的经验。这个比我强,我需要你的帮助。这似乎很容易,但我找不到解决方案,尽管这个问题已经被问到了,而且网上有很多教程。我删除了视图删除并创建了另一个。仍然没有成功。有什么帮助吗?

只有表单的输入被发回,并且可以通过绑定器传递给模型对象

如果您将
@Html.DisplayFor(model=>model.XXX)
更改为
@Html.EditorFor(model=>model.XXX)
,并使用(Html.BeginForm())将
@中的所有内容放入

它应该可以工作。您可以将输入设置为只读\禁用,以模拟
显示行为。

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

@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>
}
@model carlertrack.Web.model.Article
@{
ViewBag.Title=“删除”;
Layout=“~/Views/Shared/\u Layout.cshtml”;}

是否确实要删除此项?
@DisplayNameFor(model=>model.Title) @DisplayFor(model=>model.Title) @DisplayNameFor(model=>model.Link) @DisplayFor(model=>model.Link) @DisplayNameFor(model=>model.Source) @DisplayFor(model=>model.Source) @DisplayNameFor(model=>model.IsRead) @DisplayFor(model=>model.IsRead) @DisplayNameFor(model=>model.Notes) @DisplayFor(model=>model.Notes) @DisplayNameFor(model=>model.UserId) @DisplayFor(model=>model.UserId) @使用(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) @ActionLink(“返回列表”,“索引”,null,新{@class=“btn btn default”}) }

元素中没有任何表单控件。您想发布到哪个方法-方法
Delete()
?在这种情况下,您只传回
ArticleId
属性的值(而不是整个模型),您可以将其作为路由参数。图像中控制器的代码与发布代码中的代码不同。您的模型是
Article
,但所有方法都引用
JobAnnouncement
!我放置了只存在删除问题的正确控制器。这个问题似乎正在讨论中。现在问题解决了。为什么你认为OP或任何人需要显示一个编辑器来删除某些内容。我认为用一个删除按钮显示标签可以删除一些东西。您只需要传递要删除的项的id。这会起作用,但如果他不想显示输入,他需要传递带有隐藏字段的参数。当然,他只需要项的id。但问题是为什么模型是空的。所以我的回答解释了这一点。如果OP只想删除一篇文章,为什么你认为他们应该传递
Notes
IsRead
以及其他属性?您的建议是,当请求页面时,数据的大小基本上增加了一倍。传递所有这些不必要的值不仅毫无意义(删除一个项目只需要ID),而且会降低性能我同意只有ArticleId就足以删除一个项目。但如果答案是这样的话,它会让作者更好地理解它是如何工作的。谢谢你们的回答,谢谢你们的评论。事实上,我只需要id。但是,如果我想删除具有的文章,让;假设源代码相同,那么我必须保留@Html.HiddenFor(model=>model.source)。
[HttpPost]
public ActionResult Delete(Model.Article article)
{
    var _article = _articleReadApp.GetById(article.ArticleId);
    _articleCommandApp.Remove(_article.ArticleId);
    return RedirectToAction("Index","Article");
}
@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>
}