如果 ddl 被 jQuery 隐藏,UpdateModel 对我来说无法持续工作

发布于 2024-08-22 04:58:43 字数 1394 浏览 1 评论 0原文

我的 jQuery 代码在某些情况下隐藏了 ddl。在这种情况下,提交表单后,使用 UpdateModel 似乎无法一致工作。我在控制器中的代码:

// POST: /IllnessDetail/Edit
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(IllnessDetail ill)
        {
            IllnessDetailFormViewModel mfv = new IllnessDetailFormViewModel(ill);
            if (ModelState.IsValid)
            {
                try
                {
                    IllnessDetail sick = idr.GetLatestIllnessDetailByUsername(User.Identity.Name);
                    UpdateModel(sick);
                    idr.Save();
                    return RedirectToAction("Current", "IllnessDetail");
                }
                catch
                {
                    ModelState.AddRuleViolations(mfv.IllnessDetail.GetRuleViolations());
                }
            }
            return View(new IllnessDetailFormViewModel(ill));
        }

我刚刚开始使用 MVC,并且在截止日期前工作,所以对于 UpdateModel 的工作原理仍然很模糊。调试似乎显示正确的值被传递到了操作方法中:

public ActionResult Edit(IllnessDetail ill)

并且正确的值被放入以下行中的sick中:

IllnessDetailFormViewModel mfv = new IllnessDetailFormViewModel(ill);

但是,当一切都完成并返回给客户端时,显示的是以下值:

sick.IdInfectiousAgent

而不是值:

ill.IdInfectiousAgent

我能想到的唯一原因是 ddlInfectiousAgent 已被 jQuery 隐藏。还是我喊错了灯柱?

安德鲁

My jQuery code hides a ddl under certain circumstances. When this is the case, after submitting the form, using the UpdateModel doesn't seem to work consistently. My code in the controller:

// POST: /IllnessDetail/Edit
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(IllnessDetail ill)
        {
            IllnessDetailFormViewModel mfv = new IllnessDetailFormViewModel(ill);
            if (ModelState.IsValid)
            {
                try
                {
                    IllnessDetail sick = idr.GetLatestIllnessDetailByUsername(User.Identity.Name);
                    UpdateModel(sick);
                    idr.Save();
                    return RedirectToAction("Current", "IllnessDetail");
                }
                catch
                {
                    ModelState.AddRuleViolations(mfv.IllnessDetail.GetRuleViolations());
                }
            }
            return View(new IllnessDetailFormViewModel(ill));
        }

I have only just started with MVC, and working under a deadline, so am still hazy as to how UpdateModel works. Debugging seems to reveal that the correct value is passed in to the action method:

public ActionResult Edit(IllnessDetail ill)

And the correct value is put into sick in the following line:

IllnessDetailFormViewModel mfv = new IllnessDetailFormViewModel(ill);

However, when all is said, done and returned to the client, what displays is the value of:

sick.IdInfectiousAgent

instead of the value of:

ill.IdInfectiousAgent

The only reason I can think of is that the ddlInfectiousAgent has been hidden by jQuery. Or am I barking up the wrong lamp post?

Andrew

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

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

发布评论

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

评论(3

药祭#氼 2024-08-29 04:58:43

这似乎有点奇怪。我唯一能想到尝试的两件事是:

不要隐藏 DDL,看看是否可以解决问题。当 DDL 隐藏时,MVC 可能会忽略模型中的值。如果它被隐藏,那么它就是不需要的,这可能是合理的。

我尝试的另一件事是像这样声明你的控制器;

public ActionResult Edit(IllnessDetail ill, FormCollection formCollection)

然后,您可以从 formCollection 中提取每个值并手动插入到模型中。

所以 string myVal = formCollection["ddlName"];

不确定这些中的任何一个是否会有帮助,因为它似乎正在发生一个奇怪的错误。

如果这不能解决问题,那么也许您也可以发布 html。

希望这有帮助。

This seems a little odd. The only two things I can think of trying are;

Don't hide the DDL and see if that fixes the issue. Perhaps MVC is omitting the values in the model when the DDL is hidden. It might be rationlising that if it's hidden then it's unwanted.

The other thing I'd try is to declare your controller like this;

public ActionResult Edit(IllnessDetail ill, FormCollection formCollection)

Then you could extract each value out of the formCollection and insert into your model manually.

So string myVal = formCollection["ddlName"];

Unsure if either of these will help as it seems like an odd error to be happening.

If this doesn't fix the issue then perhaps you could also post the html.

Hope this helps.

他夏了夏天 2024-08-29 04:58:43

我已经在使用 UpdateModel 和使用 FormCollection 之间走了一半的路,如下所示:

// POST: /IllnessDetail/Edit
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(IllnessDetail ill)
        {
            IllnessDetailFormViewModel mfv = new IllnessDetailFormViewModel(ill);
            try
            {
                ill.username = User.Identity.Name;
                IllnessDetail sick = idr.GetLatestIllnessDetailByUsername(User.Identity.Name);
                sick.IdInfectiousAgent = ill.IdInfectiousAgent;
                sick.IdEncephalitisSubType = ill.IdEncephalitisSubType;
                sick.IdEncephalitisType = ill.IdEncephalitisType;
                UpdateModel(sick);
                idr.Save();
                return RedirectToAction("Current", "IllnessDetail");
            }
            catch
            {
                ModelState.AddRuleViolations(mfv.IllnessDetail.GetRuleViolations());
            }
            return View(new IllnessDetailFormViewModel(ill));
        }

具体来说,我已经手动更新了模型数据的属性,这些属性引用了根据第一个中的用户输入显示或隐藏的级联 ddls:

sick.IdInfectiousAgent = ill.IdInfectiousAgent;
sick.IdEncephalitisSubType = ill.IdEncephalitisSubType;
sick.IdEncephalitisType = ill.IdEncephalitisType;

我是知道这完全是胡扯,但我不知道为什么有必要。如您所见,我在方法调用中使用模型绑定器:

// POST: /IllnessDetail/Edit
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(IllnessDetail ill

但是,即使 ill 对象填充了正确的数据,后来对 UpdateModel(sick) 的调用也不起作用。

有谁知道为什么会发生这种情况吗?

顺便说一下,我上面说过不,我去年病的是心内膜炎,通常是心脏瓣膜感染,但一切都很好,即使最终以心脏手术结束。 ..

I have gone half way between using UpdateModel and using the FormCollection, as follows:

// POST: /IllnessDetail/Edit
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(IllnessDetail ill)
        {
            IllnessDetailFormViewModel mfv = new IllnessDetailFormViewModel(ill);
            try
            {
                ill.username = User.Identity.Name;
                IllnessDetail sick = idr.GetLatestIllnessDetailByUsername(User.Identity.Name);
                sick.IdInfectiousAgent = ill.IdInfectiousAgent;
                sick.IdEncephalitisSubType = ill.IdEncephalitisSubType;
                sick.IdEncephalitisType = ill.IdEncephalitisType;
                UpdateModel(sick);
                idr.Save();
                return RedirectToAction("Current", "IllnessDetail");
            }
            catch
            {
                ModelState.AddRuleViolations(mfv.IllnessDetail.GetRuleViolations());
            }
            return View(new IllnessDetailFormViewModel(ill));
        }

Specifically, I have manually updated the properties on the model data that refer to the cascaded ddls that get shown or hidden according to user input in the first one:

sick.IdInfectiousAgent = ill.IdInfectiousAgent;
sick.IdEncephalitisSubType = ill.IdEncephalitisSubType;
sick.IdEncephalitisType = ill.IdEncephalitisType;

I am aware that this is a complete fudge, but I have no idea why it is necessary. As you can see, I am using the model binder in the method call:

// POST: /IllnessDetail/Edit
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(IllnessDetail ill

But even though the ill object is populated with the correct data, the later call to UpdateModel(sick) does not work.

Does anyone have a clue as to why this is happening?

By the way, I stated above that I had been ill last year. No, not encephalitis. What I had was endocarditis, an infection inside the heart, usually on the heart valves. Nasty job, but all is well that ends well. Even if it ends in heart surgery...

依 靠 2024-08-29 04:58:43

刚刚发现我还没有更新这个。整个问题的原因与 jQuery 完全无关,因为服务器代码中发生了错误,该错误被 AJAX 响应“吞没”。

我最终通过在FireFox中打开页面并使用FireBug查看是否发生任何错误来检测到错误。这对某些人来说似乎是显而易见的,但我是 AJAX 新手。但后来我下载了 FireBug,发现它很好......

jQuery 实际上只是(正确地)告诉我调用没有返回任何结果。我没有想到这可能是因为返回了错误。

有问题的错误是我在升级到 MVC 2 的应用程序中使用 HttpGet,因此被新的 JsonRequestBehavior 参数 捕获,如下所示:

return Json(IllnessDetailRepository.CascadedInfectiousAgent(Id), 
            JsonRequestBehavior.AllowGet);

此参数在 MVC 1 中不存在,并且我正如他们所说,不知道它已添加到 MVC 2. RTFM 中。

Just realised I hadn't updated this. The cause of the whole issue was not related to jQuery at all, as an error was happening in the server code that was being "swallowed" by the AJAX response.

I finally detected the error by opening the page in FireFox and using FireBug to see if any errors happened. This may seem obvious to some, but I was new to AJAX. But then I downloaded FireBug and saw that it was good...

The jQuery was actually just (correctly) telling me that no results had been returned by the call. It didn't cross my mind that this might be because an error had been returned instead.

The error in question was that I was using an HttpGet in an app upgraded to MVC 2, so got snagged by the new JsonRequestBehavior parameter, as in:

return Json(IllnessDetailRepository.CascadedInfectiousAgent(Id), 
            JsonRequestBehavior.AllowGet);

This parameter didn't exist in MVC 1, and I was unaware of its adition to MVC 2. RTFM, as they say.

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