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

时间:2019-03-20 19:14:48

标签: c# asp.net-mvc

  

对象引用未设置为对象的实例。说明:   当前Web执行期间发生未处理的异常   请求。请查看堆栈跟踪以获取有关   错误及其在代码中的起源。

     

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

我在下面的线程中进行了阅读,但对于遇到的问题,我找不到任何东西。 What is a NullReferenceException, and how do I fix it? 尝试首次加载页面后,视图上发生我的错误。当我通读该页面时,它看起来都是在引用我回溯的控制器,而且它们都在引用某些内容,因此我不确定我还能做什么...

enter image description here

我收到此错误消息

@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>

1 个答案:

答案 0 :(得分:0)

考虑了培根所说的内容后,我通过简单地移动列表并将模型引用到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");
    }
}