日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

html.textboxfor id,How to update the textbox value @Html.TextBoxFor(m = m.MvcGridModel.Rows[j].Id)

發布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html.textboxfor id,How to update the textbox value @Html.TextBoxFor(m = m.MvcGridModel.Rows[j].Id) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題

I have problem, that the text box value doesn't get updated with the new value in the model.

@Html.TextBoxFor(m => m.MvcGridModel.Rows[j].Id)

First the collection MvcGridModel.Rows get populated with some data, then when press button and submit the form it get new data successfully, but it doesn't update the textbox's value.

Do you have any ideas?

Thank u in advance

回答1:

That's because HTML helpers such as TextBoxFor first look in the ModelState when binding their values and only after that in the model. So if in your POST action you attempt to modify some value that was part of the initial POST request you will have to remove it from the ModelState as well if you want those changes to take effect in the view.

For example:

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

}

This behavior is intentional and it is by design. This is what allows for example to have the following POST action:

[HttpPost]

public ActionResult Foo(MyViewModel model)

{

// Notice how we are not passing any model at all to the view

return View();

}

and yet inside the view you get the values that the user initially entered in the input fields.

There's also the ModelState.Clear(); method that you could use to remove all keys from the modelstate but be careful because this also removes any associated modelstate errors, so it is recommended to remove only values from the ModelState that you intend to modify inside your POST controller action.

All this being said, in a properly designed application you should not need this. Because you should use the PRG pattern:

[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");

}

來源:https://stackoverflow.com/questions/11341545/how-to-update-the-textbox-value-html-textboxform-m-mvcgridmodel-rowsj-id

總結

以上是生活随笔為你收集整理的html.textboxfor id,How to update the textbox value @Html.TextBoxFor(m = m.MvcGridModel.Rows[j].Id)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。