【问题标题】:Episerver HttpGet method return nullEpiserver HttpGet 方法返回 null
【发布时间】:2015-04-03 07:55:01
【问题描述】:

我有一个基于 EpiServer MVC 的项目。 PageContoller 之一有 2 种方法:

    [HttpGet]
    public ActionResult Create()
    {
        var model = new Models.Comment {Time = DateTime.Now};
        return View(model);
    }

    [HttpPost]
    public ActionResult Create(Models.Comment comment)
    {
        if (comment != null)
        {
            CommentsContainer.Add(comment);
        }
        else
        {

            var x = new Models.Comment
            {
                User = "No user",
                Body = "No text",
                Time = DateTime.Now,
            };
            CommentsContainer.Add(x);
        }
        return RedirectToAction("Index");
    }

[HttpGet] 方法有这个视图:

    @using EPiServer.Globalization
@model EPiServer.Templates.Alloy.Models.Comment
@{
    Layout = null;
}
@using (Html.BeginForm(null, null, new { language = ContentLanguage.PreferredCulture.Name }, FormMethod.Post))
{
    @Html.LabelFor(model => model.User, "User")
    @Html.EditorFor(model => model.User)
    <br /><br />
    @Html.LabelFor(model => model.Body, "Text")
    @Html.EditorFor(model => model.Body)
    <br /><br />
    <input type="submit" value="Add" />
}

但 [HttpPost] 参数始终为空。有谁知道这个问题是怎么解决的?

【问题讨论】:

    标签: asp.net-mvc controller nullreferenceexception http-get episerver


    【解决方案1】:

    您可以发布您的模型类型的代码吗?确保有 User、Body 和 Time 的公共属性。

    【讨论】:

      【解决方案2】:

      所以万一的问题是Model实现了IContent接口。创建单个ViewModel可以解决这个问题。

      【讨论】:

        猜你喜欢
        • 2015-12-16
        • 2013-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-22
        相关资源
        最近更新 更多