How to update text box value @ Html.TextBoxFor (m => m.MvcGridModel.Rows [j] .Id)

I have a problem that the value of the text field is not updated with the new value in the model. @ Html.TextBoxFor (m => m.MvcGridModel.Rows [j] .Id)

First, the MvcGridModel.Rows collection is filled with some data, then when you click the button and submit the form, it receives new data successfully, but does not update the value of the text field.

Do you have any ideas? Thanks in advance

+1
source share
1 answer

, HTML-, TextBoxFor, ModelState . , POST- - , POST, ModelState, , .

:

[HttpPost]
public ActionResult Foo(MyViewModel model)
{
    // we change the value that was initially posted
    model.MvcGridModel.Rows[0].Id = 56;

    // we must also remove it from the ModelState if
    // we want this change to be reflected in the view
    ModelState.Remove("MvcGridModel.Rows[0].Id");

    return View(model);
}

. , , , POST:

[HttpPost]
public ActionResult Foo(MyViewModel model)
{
    // Notice how we are not passing any model at all to the view
    return View();
}

, .

ModelState.Clear();, modelstate, , modelstate, ModelState, POST.

, , . PRG:

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    if (!ModelState.IsValid)
    {
        // there was some error => redisplay the view without any modifications
        // so that the user can fix his errors
        return View(model);
    }

    // at this stage we know that the model is valid. 
    // We could now pass it to the DAL layer for processing.
    ...

    // after the processing completes successfully we redirect to the GET action
    // which in turn will fetch the modifications from the DAL layer and render
    // the corresponding view with the updated values.
    return RedirectToAction("Index");
}
+3

All Articles