使用复选框/单选按钮的 EditorTemplate 获取对象引用错误

发布于 2025-01-06 17:02:19 字数 1483 浏览 1 评论 0原文

每当我在正在使用的向导上点击“返回”时,我都会收到 System.NullReferenceException:对象引用未设置为对象的实例(此外,如果我只是点击返回,我会收到“确认重新提交”页面)浏览器)。这是由于以下代码(通常,不使用此系统,在我的向导上点击“后退”或浏览器上的后退按钮就可以正常工作):

// Loop through the items and make sure they are Selected if the value has been posted
    if(Model != null)
    {
        foreach (var item in selectorModel.Items)
        {
            if (supportsMany)
            {
                var modelStateValue = GetModelStateValue<string[]>(Html, fieldName) ?? ((IEnumerable)Model).OfType<object>().Select(m => m.ToString());
                item.Selected = modelStateValue.Contains(item.Value);
            }
            else
            {
                var modelStateValue = GetModelStateValue<string>(Html, fieldName);
                item.Selected = modelStateValue.Equals(item.Value, StringComparison.OrdinalIgnoreCase);
            }
        }
    }

错误发生在 item.Selected = modelStateValue.Equals(item.Value , StringComparison.OrdinalIgnoreCase);

向导上“后退”按钮的向导代码在控制器中如下所示:

public ActionResult EMailQuoteConfirm(string backButton, string nextButton)
        {
            if (backButton != null)
                return RedirectToAction("EMailQuoteBasicDetails");
            else if (nextButton != null)
                return RedirectToAction("EMailQuoteSubmitted");
            else
                return View("EMailQuote/Confirm", myData);
        }

欢迎提供任何建议。

I am getting a System.NullReferenceException: Object reference not set to an instance of an object when ever I hit "back" on a wizard I am using (also, I am getting a "confirm resubmission" page if I just hit back on the browser). This is due to the following code (normally, without using this system hitting "back" on my wizard or the back button on the browser works fine):

// Loop through the items and make sure they are Selected if the value has been posted
    if(Model != null)
    {
        foreach (var item in selectorModel.Items)
        {
            if (supportsMany)
            {
                var modelStateValue = GetModelStateValue<string[]>(Html, fieldName) ?? ((IEnumerable)Model).OfType<object>().Select(m => m.ToString());
                item.Selected = modelStateValue.Contains(item.Value);
            }
            else
            {
                var modelStateValue = GetModelStateValue<string>(Html, fieldName);
                item.Selected = modelStateValue.Equals(item.Value, StringComparison.OrdinalIgnoreCase);
            }
        }
    }

the error happens on item.Selected = modelStateValue.Equals(item.Value, StringComparison.OrdinalIgnoreCase);

The wizard code for the "back" button on the wizard looks like this in the controller:

public ActionResult EMailQuoteConfirm(string backButton, string nextButton)
        {
            if (backButton != null)
                return RedirectToAction("EMailQuoteBasicDetails");
            else if (nextButton != null)
                return RedirectToAction("EMailQuoteSubmitted");
            else
                return View("EMailQuote/Confirm", myData);
        }

Any advice is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

五里雾 2025-01-13 17:02:19

经过对这个问题的大量研究,我确定是序列化导致了这个奇怪的错误。尽管序列化效果很好,但我还是选择了 jQuery 解决方案,该解决方案更适合此代码。

经验教训:jQuery 还不错。 :)

After much research into the issue I determined it was serialization that was causing this weird error. And although serialization worked well, I've opted for a jQuery solution which works better with this code.

Lesson learned: jQuery ain't so bad. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文