Does Html.TextBox uses Request.Params instead of Model?

[亡魂溺海] 提交于 2019-12-01 04:09:05

问题


I have a simple test application:

Model:

public class Counter
{
    public int Count { get; set; }

    public Counter()
    {
        Count = 4;
    }
}

Controller:

public class TestController : Controller
{
    public ActionResult Increment(Counter counter)
    {
        counter.Count++;
        return View(counter);
    }
}

View:

<form action="/test/increment" method="post">
    <input type="text" name="Count" value="<%= Model.Count %>" />
    <input type="submit" value="Submit" /> 
</form>

Clicking Submit I get such values:

5, 6, 7, 8, ...

With Html.TextBox I expected the same behaviour

<form action="/test/increment" method="post">
    <%= Html.TextBox("Count") %>
    <input type="submit" value="Submit" /> 
</form>

but actually got

5, 5, 5, 5.

It seems Html.TextBox uses Request.Params instead of Model?


回答1:


Html.TextBox() uses internally ViewData.Eval() method which first attempts to retrieve a value from the dictionary ViewData.ModelState and next to retrieve the value from a property of the ViewData.Model. This is done to allow restoring entered values after invalid form submit.

Removing Count value from ViewData.ModelState dictionary helps:

public ActionResult Increment(Counter counter)
{
    counter.Count++;
    ViewData.ModelState.Remove("Count");
    return View(counter);
}

Another solution is to make two different controller methods for GET and POST operations:

public ActionResult Increment(int? count)
{
    Counter counter = new Counter();

    if (count != null)
        counter.Count = count.Value;

    return View("Increment", counter);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Increment(Counter counter)
{
    counter.Count++;

    return RedirectToAction("Increment", counter);
}

Counter object could also be passed via TempData dictionary.

You may also be interested in the article Repopulate Form Fields with ViewData.Eval() by Stephen Walther.




回答2:


That's not the issue here. Specifying

<%= Html.TextBox("Count") %>

is equivalent to specifying

<%= Html.TextBox("Count", null) %>

which will pull the matching value (named "Count") from the ModelStateDictionary.

But even so, explicitly passing in

<%= Html.TextBox("Count", Model.Count) %>

results in the same behavior described by alex2k8.




回答3:


Html.TextBox has more parameters than one..first parameter is the name or id of input element, and the second one is the value...

so write your textbox helper like this:

<%= Html.TextBox("Count",Model.Count) %>

cheers



来源:https://stackoverflow.com/questions/953330/does-html-textbox-uses-request-params-instead-of-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!