1

Controller:

public class HomeController : Controller
            {
                //
                // GET: /Home/
                public ActionResult Index()
                {   
                    return View();
                }
                [HttpPost]
                public ActionResult Index(string list1)
                {
                    return View();
                }


            }

View(uses default layout):

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm())
{

    @Html.DropDownList("list1",
                        new SelectList(new[] { "Item 1", "Item 2", "Item 3" }))

    <input type="submit"/>
}
<br />
<br />

After selecting value in DropDownList and submitting the form my application have same state that was before. For example if i had selected "item 3" after pressing "submit" i have selected "item 3". How does it work?

Upd:

I dont want to avoid it, just want to understand how its work.

  • This is an issue(means you are not able to modify your dropdown selection item) or you are asking how this is happening? – Rajput Oct 22 '16 at 19:03
  • No, i just want to know how working mechanism of state keeping, cause as we know "web is stateless". May be this "helpers" uses ModelState for it, i don`t know. – Vlad Melnik Oct 22 '16 at 19:11
  • 1
    Please confirm whether the request is send to `server` on `submit` – L J Oct 22 '16 at 19:14
  • Yes, and processed by `ActionResult Index(string list1)` – Vlad Melnik Oct 22 '16 at 19:25
  • When you press button _Submit_, selected value of your select simply sends to `Index` method. Note that name of select and `string` parameter in `Index` method must be equal. – Yurii N. Oct 22 '16 at 20:00

2 Answers2

2

In your [Post] handler you are not redirecting anywhere and returning a view directly. Hence the values persist because they are still found in the model state.

A common pattern is to redirect to a [Get] handler after a successful Post, in which case values would not persist.

Community
  • 1
  • 1
GSerg
  • 76,472
  • 17
  • 159
  • 346
  • @VladMelnik It also might be the browser restoring values in controls after page refresh. Set a breakpoint in the view and see if it has a value in its modelstate on the server side. – GSerg Oct 22 '16 at 22:33
  • [Yes, it is(]http://image.prntscr.com/image/6b73430b8bf14614b403c9eb069d4502.png) – Vlad Melnik Oct 22 '16 at 22:40
  • @VladMelnik Apparently the code you posted is not your actual code. On your screenshot the form is `method="get"`, so your `HttpPost` handler is never triggered, and the values persist because with `get` forms they are passed in the URL. In the code you posted, the form is `method="post"` (by default) so the `HttpPost` handler is triggered and the parameters are not passed in the URL. – GSerg Oct 23 '16 at 07:28
0

From MVC sources:

SelectExtensions.cs:

.....
    object defaultValue = (allowMultiple) ? htmlHelper.GetModelStateValue(fullName, typeof(string[])) : htmlHelper.GetModelStateValue(fullName, typeof(string));
.....

HtmlHelper.cs:

internal object GetModelStateValue(string key, Type destinationType)
        {
            ModelState modelState;
            if (ViewData.ModelState.TryGetValue(key, out modelState))
            {
                if (modelState.Value != null)
                {
                    return modelState.Value.ConvertTo(destinationType, null /* culture */);
                }
            }
            return null;
        }

Well, it means that Action sends "ModelState" to view and controls(i know that it's not correct definition) use "ModelState" for fill themselves.