Route ID Overrides Model.Id

I have a route like this:

Conference/Committees/1

And on this page, it goes through the conference committees (where conference id = 1).

I have a partial view that displays an edit style page for a selected committee with the same way:

Conference/Committees/1?committeeId=2

When debugging, these models are correct, and the Committee has Id = 2. However, when I use the following Razor statement:

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

with the following model:

@model munhq.Models.Committee

Hidden input has a value of "1" instead of "2".

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

Update

If I replaced

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

from

<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" />

It displays the correct Id value.

Update 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();
            }
        }
    }
+4
source share
1

, :

ModelState.Clear();

, a Action , a ModelStateCollection , , .. ModelStateCollection View. HTML ModelStateCollection, . , Html.HiddenFor ( ModelStateCollection), <input type="hidden"> .

+7

Source: https://habr.com/ru/post/1523762/


All Articles