简体   繁体   English

POST操作后,复杂模型为空

[英]Complex model is null after POST action

There are a lot of posts about this problem but I still can't find the correct solution. 关于此问题的帖子很多,但我仍然找不到正确的解决方案。 Currently my model, view and controller look like this: 目前,我的模型,视图和控制器如下所示:

View: 视图:

@model Pro.Web.Models.CatDetailsView

@using (Html.BeginForm("Details", "Cat", Model, FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()

<h2>Details</h2>

<ol>
    <li>
        @Html.LabelFor(model => model.Cat.Name)
        <p>@Html.DisplayFor(model => model.Cat.Name, new { @readonly = "true" })</p>
    </li>

    <li>
        @Html.LabelFor(model => model.Cat.Owner)
        <p>@Html.DisplayFor(model => model.Cat.Owner.UserName, new { @readonly = "true" })</p>
    </li>

    <li>
        @Html.LabelFor(model => model.Cat.BornDate)
        <p>@Html.DisplayFor(model => model.Cat.BornDate, new { @readonly = "true" })</p>
    </li>

    <li>
        @Html.LabelFor(model => model.Cat.Age)
        <p>@Html.DisplayFor(model => model.Cat.Age, new { @readonly = "true" })</p>
    </li>


    <li>
        @Html.LabelFor(model => model.Cat.CatColorValue)
        <p>@Html.DisplayEnumFor(model => model.Cat.CatColorValue, typeof(Pro.Web.Models.Entities.Enums.CatColor))</p>
    </li>

    <li>
        @Html.LabelFor(model => model.Cat.Description)
        <p>@Html.DisplayFor(model => model.Cat.Description, new { @readonly = "true" })</p>
    </li>

</ol>

<input type="submit" value="Edit details" name="Command" />

}

Model: 模型:

public class CatDetailsView
{
    public Cat Cat { get; set; }
}

Controller: 控制器:

[HttpPost]
    public ActionResult Details(CatDetailsView model, string Command)
    {
        if (Command.Equals("Edit details"))
        {
            Cat cat = this._catRepository.Find(model.Cat.Id);
            if (cat != null)
            {
                return RedirectToAction("Edit", "Cats", new { id = cat.Id });
            }
            else
            {
                return View(model);
            }
        }

        else
        {
            return View(model);
        }

    }

The problem is that controller always receives null for model. 问题在于,控制器始终为模型接收空值。 I'm looking in that code and can't see what is wrong. 我正在查看该代码,看不到有什么问题。 I've just began learning so I am probably missing something trivial. 我刚刚开始学习,所以我可能缺少一些琐碎的东西。 I hope someone has an idea what could be a problem. 我希望有人知道可能是个问题。

Thank you. 谢谢。

You are using DisplayFor instead of EditorFor, so you're not going to be submitting anything. 您正在使用DisplayFor而不是EditorFor,因此您将不会提交任何内容。

DisplayFor is used to display data. DisplayFor用于显示数据。

EditorFor is used to add form input elements such as text boxes, radio buttons, check boxes, etc. EditorFor用于添加表单输入元素,例如文本框,单选按钮,复选框等。

edit 编辑
sormii mentioned this in the comments so I will add it to the answer. sormii在评论中提到了这一点,因此我将其添加到答案中。 If you want the user to be able to change the values that are getting posted, then you should use EditorFor, but if you want to just show them the data (not allow them to change) then you can use HiddenFor right after each DisplayFor. 如果希望用户能够更改要发布的值,则应使用EditorFor,但如果只想向他们显示数据(不允许他们更改),则可以在每个DisplayFor之后使用HiddenFor。 This will put a hidden input that will get posted. 这将放置一个将被发布的隐藏输入。

It looks like you are trying to use readonly=true, so I think you might want to use TextBoxFor in order to do that. 看来您正在尝试使用readonly = true,所以我认为您可能想使用TextBoxFor来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM