Route Id 覆盖 Model.Id [英] Route Id overrides Model.Id

查看:26
本文介绍了Route Id 覆盖 Model.Id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的路线:

Conference/Committees/1

在该页面中,它会循环显示会议的委员会(其中会议 ID = 1).

And within that page, it cycles through the Committees for a Conference (where the Conference Id = 1).

我有一个局部视图,它为选定的委员会呈现编辑样式页面,路径如下:

I have a partial view that renders an edit style page for a selected committee, with a route like this:

Conference/Committees/1?committeeId=2

在调试中,模型数据是正确的,委员会有一个Id = 2.但是,当我使用以下Razor语句时:

In debug, the model data is correct, and the Committee has an Id = 2. However, when I use the following Razor statement:

@Html.HiddenFor(model => model.Id)

使用以下型号:

@model munhq.Models.Committee

隐藏输入的值是1"而不是2".

The hidden input has an value of "1" instead of "2".

这是 MVC 中的错误吗?还是我做错了什么?

Is this a bug in MVC? Or am I doing something wrong?

更新

如果我替换

@Html.HiddenFor(model => model.Id)

<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Id" name="Id" type="hidden" value="@Model.Id" />

它呈现正确的 Id 值.

it renders the correct Id value.

更新 2

    public async Task<ActionResult> Committees(int id, PrivilegedAction? actionToTake, int? committeeId, ConferenceStatusMessage? csm)
    {
        Conference conference;
        HandleConferenceStatusMessage(csm);

        try
        {
            conference = await db.Conferences
            .Include(i => i.Committees.Select(c => c.CommitteeMemberCommitteeEntries))
            .Where(i => i.Id == id)
            .SingleAsync();

            HandleAction(actionToTake, conference);
            HandleAuthorisations(conference);
        }
        catch
        {
            return ConferenceActionFail();
        }

        if (committeeId == null)
        {
            if (conference.Committees.FirstOrDefault() == null)
            {
                committeeId = 0;
            }
            else
            {
                committeeId = conference.Committees.FirstOrDefault().Id;
            }

            ViewBag.ConferenceId = id; // used for adding a committee member entry

            return RedirectToAction("Committees", new { id = id, action = actionToTake, committeeId = committeeId, csm = csm });
        }
        else
        {
            if (CommitteeIsPartOfConference(conference, committeeId) || committeeId == 0)
            {
                ViewBag.SelectedCommittee = committeeId;
                ViewBag.JsonAvailableMembers = jsonAvailableCommitteeMembers(id);

                return View(conference);
            }
            else
            {
                return HttpNotFound();
            }
        }
    }

推荐答案

在返回视图之前尝试使用这个:

Try using this before returning a view:

ModelState.Clear();

一般来说,当一个 Action 被调用时,框架会根据查询字符串值、post-data、路由值等构建一个 ModelStateCollection.而这个 ModelStateCollection 将被传递给 View.所有的 HTML 输入助手首先尝试从 ModelStateCollection 中获取值,然后再尝试从实际模型中获取值.这就是为什么您的 Html.HiddenFor 扩展工作不正确(它首先检查 ModelStateCollection)而您的 <input type="hidden"> 包含正确值的原因.

In general when an Action gets called the framework builds a ModelStateCollection based on the query-string values, post-data, routing values etc. And this ModelStateCollection will be passed to the View. All the HTML input helpers try to the get the values from the ModelStateCollection first, before trying to get the values from the actual model. And that's the reason why your Html.HiddenFor extension works incorrectly (it checks for ModelStateCollection firstly) whereas your <input type="hidden"> contains a correct value.

这篇关于Route Id 覆盖 Model.Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆