ASP.Net MVC ModelState / Html.TextBox postback issue

梦想与她 提交于 2019-12-12 08:13:07

问题


I have an issue cropping up in a form I'm trying to post. In the scenario where the form doesn't validate, I'm taking the standard route of calling ModelState.AddModelError() then returning a View result.

The thing is, the HTML.* helpers are supposed to pick up the posted value when rendering and I'm noticing that my text fields ONLY do so if I include them in the parameter list of the postback action, which shouldn't be required seeing as some forms have way too many fields to want to list them all as parameters.

My action code is roughly:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditDataDefinition(long? id, string name)
{
    var dataDefinition = ...

    // do some validation stuff
    if (!ModelState.IsValid)
    {
        // manually set checkbox fields via ViewData seeing as this STILL doesn't work in MC 1.0 :P
        // ...
        return View(dataDefinition);
    }

}

Now, dataDefinition (which is a LINQ to SQL entity) has a field MinVolume, is handled in the view by this line:

Minimum: <%= Html.TextBox("MinVolume", null, new { size = 5 })%>

Yet when the view is rendered after a failed ModelState validation, the value typed into it on the original page we posted is not preserved UNLESS I include it as a parameter in the postback method. Literally, I can "solve the problem" by doing this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditDataDefinition(long? id, string name, string minVolume)

For some reason that will force the field value to be preserved. This seems stupid to me because my form has way more values than just that and I shouldn't have to add a parameter for just that field.

Any ideas?


回答1:


Could it be that your code:

<%= Html.TextBox("MinVolume", null, new { size = 5 })%>

..has the null for the default value param? Maybe if you change the null to Model.MinVolume it will persist the value. Like this:

<%= Html.TextBox("MinVolume", Model.MinVolume, new { size = 5 })%>

I'm not sure if your action returns the value MinVolume in the model tho. If it does, the above should work. Else, you may need to refactor the action slightly.




回答2:


Oh man I've just improved my application design. The problem occurs because you have custom validation (I have too). You have to add after

ModelState.AddModelError()

this

ModelState.SetModelValue("MinVolume", ValueProvider["MinVolume"]);

In view it has to be

Mimum:<%=Html.Textbox("MinVolume")%>

Still not sure why it works but it worked for me.




回答3:


What is the key you are using when you set the value in the ModelState on error? The code that sets the value parameter for a TextBox looks like:

Relevant portion of the downloaded framework code.

string attemptedValue = (string)htmlHelper.GetModelStateValue(name, typeof(string));
tagBuilder.MergeAttribute("value", attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(name) : valueParameter), isExplicitValue);

As you can see if the attempt value exists, it will use it -- but only if the same key is available.

I know that this works because I have an action that takes no parameters and gets the values directly from the ValueProvider and it uses AddModelError to indicate validation errors. I'm sure that the values in my TextBoxes are retained.

EDIT: In order for the values to be retained, they need to be associated with the model in some way. One way to do this is to add them to the parameter list. Another way is to use UpdateModel (with the parameter names in the whitelist or no whitelist). A third way is to add the parameter explicitly to the model as in @Jenea's answer. Since the helper only pulls from the model state, they must be in there for the values to be retained. It does not look at the request's Form property.




回答4:


As I understand the solution is:

[Transaction]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditDataDefinition(int id, FormCollection form)
{
    T itemToUpdate = repository.Get(id);
    UpdateModel(itemToUpdate, form.ToValueProvider());

    if (itemToUpdate.IsValid())
    {
        repository.SaveOrUpdate(itemToUpdate);
        return Json(ValidationResultToJson(itemToUpdate.ValidationResults()));
    }

    repository.DbContext.RollbackTransaction();
    return Json(ValidationResultToJson(itemToUpdate.ValidationResults()));
}

good luck!




回答5:


Another solution is if you type in view:

<%var minVolume=Request["MinVolume"]??"";%>
<%=Html.Textbox("MinVolume",minVolume,new {size=5})%>



回答6:


Please refer to this article to see different ways to handle postback in ASP.NET MVC

link text



来源:https://stackoverflow.com/questions/834842/asp-net-mvc-modelstate-html-textbox-postback-issue

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