2

I'm new to MVC 3 and I have the following views:

Index:

@using (Html.BeginForm())
{
    @Html.Hidden("id", "1")
    <input type="submit" value="Submit" />
}

Main:

@using (Html.BeginForm())
{
    @Html.Hidden("id", "2")
    <input type="submit" value="Save" />
}

And the following Controller:

public ActionResult Index()
{
    ViewBag.Message = "Welcome!";
    return View();
}
[HttpPost]
public ActionResult Index(string id)
{
    if ("1".Equals(id))
    {
        return View("Main");
    }
    else("2".Equals(id))
    {
        return View();
    }
}

My expectation would be that the Main view would render the hidden input with a value of "2". However, upon reaching the Main page, and inspecting the source, the value is still being set to "1". Any idea what I'm doing wrong here?

svick
  • 236,525
  • 50
  • 385
  • 514
holic87
  • 791
  • 2
  • 17
  • 29

2 Answers2

1

That depends on the URL you are using. If you are entering ..../Index, then it will go to the Index view, but if you are going to .../Index/1 then it will go to the Main view and render your two (which is kind of odd because a 1 goes to 2 :))

Also, you will only hit the Index(string id) method on a POST, so that might also be your problem. a typical URL request comes across as a GET

If that does not help, then you might need to provide more details (what URL you are attempting to hit, and how you are trying to get there)

UPDATE AFTER TRYING THIS MYSELF

I see what you are saying. The reason for the hidden field being set to 1 is because the ModelState plugs it in for you. As far as I can tell, this should NOT happen since you are explicitly setting the value. However, it appears that if there is a matching state item, it will use that instead. You can test this by changing from using id to anything else in your Main's hidden input name.

Here is the documentation. In the remarks, it does state that this is meant more for model binding, but I would have thought that the value inserted would override anything else.

FINAL UPDATE

It turns out that this actually has already been brought up to the appropriate person, and it is by design. They SHOULD change the documentation to make this more explicit, though. Here is the SO question that answers this

Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • I am first trying to navigate to `.../Index`. Upon submitting the form on Index, it should `POST` to the Controller's `Index(string id)` method and return the Main view, which it currently does. However, the problem is that the Main view's source still has the hidden input field value set to "1". I need to be able to navigate between the pages of this site without the URL changing. Does that help? – holic87 Mar 16 '12 at 16:07
  • Interesting, I have updated my answer after trying this myself. If you ask me, this is a bug...but I am curious now, and doing some research :) – Justin Pihony Mar 16 '12 at 16:26
  • Re-Edited with what I found to be the reasoning. +1 to your question for a "noob" running into such an interesting issue :) – Justin Pihony Mar 16 '12 at 16:33
  • Yep, that seems to describe exactly why I'm running into this issue. Thanks for doing the research and linking to the detailed answer. Guess I'll just go with naming my hidden input fields different names. – holic87 Mar 16 '12 at 16:39
  • 2
    If your intent was to define the value on each page you can simple use the standard HTML to resolve the binding issue i.e. . – Michael Gattuso Mar 16 '12 at 16:49
  • @MichaelGattuso Thanks... that's what I ended up doing. I was just confused as to why it was happening in the first place. – holic87 Mar 16 '12 at 16:51
0

Don't forget the routing engine is set by default to treat the third segment as ID. If I not mistaken the route parameter will win over the form parameter so:

Case 1:

URL: controller/index/
HIDDEN_ID: 1
= Action id parameter = 1 //from the hidden field

Case 2:

URL: controller/index/2
HIDDEN_ID: 1
= action id parameter = 2 //from the url route not te hidden field
Michael Gattuso
  • 13,020
  • 2
  • 25
  • 29
  • Thanks for the info. I understand what you're saying, but in my case the hidden form ID value on the Main view isn't even being set to the correct value when the page renders. I'm expecting the `@Html.Hidden("id", "2")` line to generate `` on Main, but instead the value is "1". Thoughts? – holic87 Mar 16 '12 at 16:20
  • Came back to say the same thing as Justin - added some additonal thougts to his post. – Michael Gattuso Mar 16 '12 at 16:49