路线ID覆盖Model.Id [英] Route Id overrides Model.Id

查看:74
本文介绍了路线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值.

更新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 时,框架会基于查询字符串值,后置数据,路由值等来构建 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.

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

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