首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >即使使用IsNullOrWhiteSpace,对象引用仍然返回null吗?

即使使用IsNullOrWhiteSpace,对象引用仍然返回null吗?
EN

Stack Overflow用户
提问于 2019-03-21 03:14:48
回答 1查看 74关注 0票数 -2

对象引用未设置为对象的实例。说明:当前web请求执行过程中发生未处理异常。请查看堆栈跟踪,以了解有关错误的更多信息以及错误在代码中的来源。

异常详细信息: System.NullReferenceException:未将对象引用设置为对象的实例

我阅读了下面的帖子,但我找不到任何关于我遇到的问题的东西。What is a NullReferenceException, and how do I fix it?在第一次尝试加载页面后,视图上出现错误。当我通读那个页面时,它看起来都引用了我回溯的控制器,它们都引用了一些东西,所以我不确定我还能做什么……

我在以下位置收到此错误

@foreach (string pg in Model.Paygroups)

在我看来。我有点困惑,因为我指定了支付组,并且在我的控制器中,我将列表发送到模型。我已经尝试了几个小时了,但是我没有看到我没有引用这个对象的地方。任何帮助都是非常感谢的。

控制器

    public class UpdateFilesController : Controller
    {
        // GET: Default
        public ActionResult Edit()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Edit(string Paygroup)
        {

            var fullpath = Path.Combine(Server.MapPath("~/sourcefiles"), "paygroup.txt");
            List<string> paygroupsList = new List<string> { System.IO.File.ReadAllText(fullpath) };
            if (ModelState.IsValid)
            {
                paygroupsList.Add(Paygroup);
                ViewBag.Message($"{Paygroup} succesfully added");
            }
            if (String.IsNullOrWhiteSpace(Paygroup))
            {
                UploadFiles model = new UploadFiles
                {
                    Paygroups = paygroupsList
                };
                return View(model);
            }
            return View();
        }
    }
}

模型

    public class UploadFiles
    {
        public List<string> Paygroups { get; set; }

        [Required(ErrorMessage = "Please enter a paygroup.")]
        public string PayGroup { get; set; }

    }

视图

@model WebApplication2.Models.UploadFiles
@{
    ViewBag.Title = "Paygroup Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Update Paygroup</h2>

@using (Html.BeginForm("Edit", "UpdateFiles", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
    @Html.AntiForgeryToken()
    <div class="form-group">
        @Html.LabelFor(m => m.PayGroup, new {@class = "control-label"})
        @Html.EditorFor(m => m.PayGroup, new {htmlAttributes = new {@class = "form-control", placeholder = Html.DisplayNameFor(m => m.PayGroup)}})
        @Html.ValidationMessageFor(m => m.PayGroup, "", new {@class = "text-danger"})
        <input type="submit" value="Add" class="btn btn-default"/>
    </div>
}
@if (ViewBag.Message != null)
{
    {
        <script type="text/javascript">
            alert("@ViewBag.Message");
        </script>
    }
}
<table class="table table-striped">
    <thead>
    <tr>
        <th>Paygroups</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        @foreach (string pg in Model.Paygroups)
        {
            <td>@pg</td>
        }
    </tr>
    </tbody>
</table>
EN

回答 1

Stack Overflow用户

发布于 2019-03-21 03:49:27

考虑到Bacon所说的话之后,我通过简单地移动我的列表并将我的模型引用到我的GET edit()来解决我的问题。

public class UpdateFilesController : Controller
{
    // GET: Default
    public ActionResult Edit()
    {
        var fullpath = Path.Combine(Server.MapPath("~/sourcefiles"), "paygroup.txt");
        List<string> paygroupsList = new List<string> { System.IO.File.ReadAllText(fullpath) };
        UploadFiles model = new UploadFiles
        {
            Paygroups = paygroupsList
        };
        return View(model);
    }
    [HttpPost]
    public ActionResult Edit(string Paygroup)
    {
        if (ModelState.IsValid)
        {
            var fullpath = Path.Combine(Server.MapPath("~/sourcefiles"), "paygroup.txt");
            System.IO.File.WriteAllText(fullpath, Paygroup);
            ViewBag.Message = ($"{Paygroup} succesfully added");
        }
        return RedirectToAction("Edit", "UpdateFiles");
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55268577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档