ASP.Net MVC ModelState / Html.TextBox for postback

I have a problem arising in the form I'm trying to post. In a scenario where the form is not validated, I take the standard call route ModelState.AddModelError()and then return the result of the scan.

The fact is that HTML. * helpers must collect the published value when rendering, and I notice that my text fields ONLY do this if I include them in the list of postback action parameters, t is required because some forms have too many fields to list them as parameters.

My action code is approximately:

[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 object) has a MinVolume field, processed in the view by this line:

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

, ModelState, , , , . UNLESS . " ", :

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

- . , , , .

?

+5
6

, :

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

.. null ? , null Model.MinVolume, . :

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

, MinVolume tho. , . , .

+4

, , . - , ( ).

ModelState.AddModelError()

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

,

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

, , .

+6

, , ModelState ? , TextBox, :

.

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

, , , .

, , , ValueProvider, AddModelError . , .

EDIT. , - . - . - UpdateModel ( ). - , @Jenea. , , . .

+3

, :

[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()));
}

!

0

- :

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

, , ASP.NET MVC

0
source

All Articles